Blocking
-
Hello i'm using a neverending loop to read informations from the COM but the problem is that the reading process blocks my aplication. Not to listen to the COM all the time is not an option. What should I do to use the aplication in this time?
Create a worker thread and put your
do { //your COM reading code }while;
loop inside the Thread function.
--[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:
-
Create a worker thread and put your
do { //your COM reading code }while;
loop inside the Thread function.
--[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:
and how do i create a worker thread?
-
and how do i create a worker thread?
simple... Follow these steps : you have a function.
ReadCOM()
. You dont want this to block your application. declare these in your header may be COMPROG.h//COMPROG.h static UINT MyThread(LPVOID lp); void ReadCOM();
in the implementation,COMPROG.cpp AfxBeginThread(MyThread,this);// Your call to start the thread.
Your Thread:UINT MyThread(LPVOID lp) { CCOMPROG* obj = (CCOMPROG*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->ReadCOM(); }
Your Fcuntion:void ReadCom() { do { //Your Code to read COM }while(x==true);//your condition }
-
simple... Follow these steps : you have a function.
ReadCOM()
. You dont want this to block your application. declare these in your header may be COMPROG.h//COMPROG.h static UINT MyThread(LPVOID lp); void ReadCOM();
in the implementation,COMPROG.cpp AfxBeginThread(MyThread,this);// Your call to start the thread.
Your Thread:UINT MyThread(LPVOID lp) { CCOMPROG* obj = (CCOMPROG*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->ReadCOM(); }
Your Fcuntion:void ReadCom() { do { //Your Code to read COM }while(x==true);//your condition }
Thanks a lot I folowed your code but ended up with the folowing error message : error C4716: 'CMina_sView::MyThread' : must return a value; i tryed to insert a return 0 or return TRUE but that wasn't much of a help as I got the same result: program blocked. so??!?
-
and how do i create a worker thread?
tanarnelinistit wrote:
and how do i create a worker thread?
http://www.flounder.com/workerthreads.htm[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
Thanks a lot I folowed your code but ended up with the folowing error message : error C4716: 'CMina_sView::MyThread' : must return a value; i tryed to insert a return 0 or return TRUE but that wasn't much of a help as I got the same result: program blocked. so??!?
it has a return type of UINT so do this, I missed it :(
return(0);
Now it should work. Otherwise you tell me. we'll kill it. also that , when you put your "readCOM()" function inside the class, implement it with your class(CCOMPROG) refernce likevoid CCOMPROG::readCOM()
--[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:
-
it has a return type of UINT so do this, I missed it :(
return(0);
Now it should work. Otherwise you tell me. we'll kill it. also that , when you put your "readCOM()" function inside the class, implement it with your class(CCOMPROG) refernce likevoid CCOMPROG::readCOM()
--[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:
I'm afraid someone will have to get killed. :)) Let me tell you how i modified your code: 1) I use an OnListenComm() function that is as the name tells us: a function auto-defined by VC++ that handles the code when you click on listencom button. 2)The modifications: static UINT MyThread(LPVOID lp); afx_msg void OnListenCom1(); AfxBeginThread(MyThread,this);// Where should i put this??????? UINT CMina_sView::MyThread(LPVOID lp) { CMina_sView* obj = (CMina_sView*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->OnListenCom1(); return 0; }
-
Hello i'm using a neverending loop to read informations from the COM but the problem is that the reading process blocks my aplication. Not to listen to the COM all the time is not an option. What should I do to use the aplication in this time?
Read here.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
I'm afraid someone will have to get killed. :)) Let me tell you how i modified your code: 1) I use an OnListenComm() function that is as the name tells us: a function auto-defined by VC++ that handles the code when you click on listencom button. 2)The modifications: static UINT MyThread(LPVOID lp); afx_msg void OnListenCom1(); AfxBeginThread(MyThread,this);// Where should i put this??????? UINT CMina_sView::MyThread(LPVOID lp) { CMina_sView* obj = (CMina_sView*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->OnListenCom1(); return 0; }
It depends on when you want to start the thread. For example, you have CButton , If you wanna start listening to COM on the click of it, put the AfxBeginThread inside the OnClick Event of the CButton. You can do that anywhere.
--[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:
-
Hello i'm using a neverending loop to read informations from the COM but the problem is that the reading process blocks my aplication. Not to listen to the COM all the time is not an option. What should I do to use the aplication in this time?
You can also poll the COM port; this isn't necessarily the nicest design, but it may work. See the function SetCommTimeouts to set read timeouts. In fact, you may want to do this in either case; your thread can block reading the port and wake up every 1s to check if it is time to quit the application. Save yourself a bunch of hassle and do a bit of reading about thread safety issues now before you do a bunch of work. earl