Pray god help!!!!!! When initializing this window, initialize the parameters of the thread, then enable threads, obtain relevant serial data, but why click close, and then into an infinite loop, a thread can shut down?
CodePudding user response:
Beg god for help
CodePudding user response:
Add a thread to exit the tag, CommThread judgment in this tag while (! BThreadExit) {... }
CodePudding user response:
There is a end_thread exit sign
CodePudding user response:
refer to the second floor zgl7903 response: add a thread to exit the tag, CommThread judgment in this tag while (! BThreadExit) {... } have a endtgread mark, at the time of exit setevent already CodePudding user response:
Please copy the text, pictures don't! CodePudding user response:
reference 5 floor schlafenhamster reply: please copy text, pictures don't! since the code belongs to a more intimate environment, the computer can't get to the Internet, cannot be inserted u disk, so please forgive me CodePudding user response:
Paste the code BOOL Sdcs... OnInitDialog () { CDialogEx: : OnInitDialog (); //initialize the parameters of the thread InitEvent (); StartMonitoring (); } Void Sdcs... InitEvent () { M_Start=CreateEvent (NULL, TRUE, FALSE, NULL); M_Pause=CreateEvent (NULL, TRUE, FALSE, NULL); M_EndThread=CreateEvent (NULL, TRUE, FALSE, NULL); M_hEventArray [0]=m_Start; M_hEventArray [1]=m_Pause; M_hEventArray [2]=m_EndThread; M_bThreadAlive=FALSE; } //start the thread BOOL Sdcs... StartMonitoring () { if (! (m_Thread=AfxBeginThread (CommThread, this))) { return FALSE; } ResetEvent (m_EndThread); TRACE (" Thread started \ n "); return TRUE; } UINT Sdcs: : CommThread (LPVOID pParam) { PParam Sdcs * mycmd=(Sdcs *); DWORD Event=0; Mycmd - & gt; M_bThreadAlive=TRUE; While (1) { Event=WaitForMultipleObjects (3, mycmd - & gt; M_hEventArray, FALSE, 0); If (Event=WAIT_TIMEOUT) {using real-time display data from serial port to receive overtime} Else if (Event==0) { } Else if (Event==1) { } Else if (Event==2) { ResetEvent (mycmd - & gt; M_EndThread); break; } The else { break; } } Mycmd - & gt; M_bThreadAlive=FALSE; return 0; } Void Sdcs... OnClose () { //TODO: add the message handler code and/or invoke the default CDialogEx: : OnClose (); If (m_IsOpen) { While (m_bThreadAlive) { SetEvent (m_EndThread); } M_port. PortClose (); } M_IsOpen=FALSE; } Then click on close, direct the cling cycle; Each great god help! Genuflect is begged!!!!! CodePudding user response:
While (m_bThreadAlive) this sentence? AfxBeginThread (CommThread, this))) can be automatically quit, (see 1st floor), with too many bad event! Refer to //use ON_MESSAGE (a, b) Void CUload: : OnCommNotify (WPARAM WPARAM, LPARAM LPARAM) { //TODO: Add your specialized code here and/or call the base class Int nLength; BYTE abIn [6000].//for 4096 and 115200 needs more DWORD totalrecv; Cstrings TXT. The switch (wParam) { Case 0://the from CommWatchCTS () SetCTSIcon (); SetEvent (m_hPostEventRead);//tell watcher that CommNotify has had done break; Case 1://the from CommWatchRead () //append on each entry CFile uldFile (UpLoadFileName CFile: : modeCreate | CFile: : modeNoTruncate | CFile: : modeWrite); UldFile. SeekToEnd (); If (m_bConnected) { If (nLength=ReadCommBlock (abIn, BUFLEN))//38400 needs 1024 {//save into the file if (! M_bStart) {//only 1 st M_bStart=1; InforOut (" identifiers "+ UpLoadFileName); } UldFile. Write (abIn, nLength); } } UldFile. Close (); //get the total bytes received M_uldINs. GetText (0, TXT); Totalrecv=atol (. TXT GetBuffer (40)); Totalrecv +=nLength; TXT Format (" % d ", totalrecv); M_uldINs. ResetContent (); M_uldINs. AddString (TXT); If (nLength> 128) { The Animate (); //reset time "waiting" M_Elapse. ResetContent (); M_Elapse. AddString (" 0 s "); } //tell watcher that CommNotify has had done SetEvent (m_hPostEventRead); break; } } CodePudding user response:
You only use a SetEvent, then waiting for the Thread exits, WaitForSingleObject (hThread, INFINITE); CodePudding user response:
SetEvent this code written not so good. Read and should Wait Wait Can use variables to control! But variable is volatile! Pay attention to this! Otherwise can change variables, another thread to old data. Register the cache. CodePudding user response:
The change of volatile m_bThreadAlivenullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull