Home > Software engineering >  MFC a thread can run normally. Close. After open again. Why can't run?
MFC a thread can run normally. Close. After open again. Why can't run?

Time:09-15

A thread can run normally. After closed. Open again. Why can't run? Is there any way to open again like the first time to open the run normally. Novice consult.

//open the thread
Void CThread_lp: : Stort ()//
{
DWORD dwThreadID;
HANDLE htread=NULL;
Htread=: : CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) ThreadProc, this, 0, & amp; DwThreadID);
M_hthread_lp=htread;//
}

//close the thread
Void CThread_lp: : Stop ()
{
If (m_hthread_lp)//if the thread is
{
M_bExit=true;//set signal
: : WaitForSingleObject (m_hthread_lp, INFINITE);//waiting thread end
: : CloseHandle (m_hthread_lp);
M_hthread_lp=NULL;
}

CodePudding user response:

What is not running? The thread cannot start? GetLastError () wrong information

CodePudding user response:

Thank zgl7903 reply. Can't run. No response. I am a serial port receives the data. The first open the program. Can receive. After close the thread. Open it again. We won't be able to accept data. In the cycle of thread added a pop-up confirmation dialog prompt. You can know on the second. There is no thread of execution cycle. But open the thread's process and there is no error. So I don't know why there is no real execution

CodePudding user response:

After each create a thread GetLastError () returns a value of 0

CodePudding user response:

Void CThread_lp: : Stop ()
{
If (m_hthread_lp)//if the thread is
{
M_bExit=true;//set signal
: : WaitForSingleObject (m_hthread_lp, INFINITE);//waiting thread end
: : CloseHandle (m_hthread_lp);
M_hthread_lp=NULL;
}

This code can you close the thread?

CodePudding user response:

Hi worldy. After this code. The thread does not run. But whether it is closed. I also don't know. Could you tell me how should completely shut down

CodePudding user response:

 
//run/stop
Void CCEventDlg: : OnButton1 ()
{
The static BOOL stop=FALSE;
if(! Stop)
{
GetDlgItem (IDC_BUTTON1) - & gt; SetWindowText (" pause ");
If ((m_threaddraw==0) & amp; & (m_threadcomm==0))
{
M_threaddraw=AfxBeginThread (AFX_THREADPROC (draw), (LPVOID) this,
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
//afxDump & lt; M_threadcomm=AfxBeginThread (AFX_THREADPROC (ReadComm), (LPVOID) this,
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
# ifdef CHECK_EXIT_CODE
DuplicateHandle (GetCurrentProcess (), m_threaddraw - & gt; M_hThread, GetCurrentProcess (),
& M_htmpcomm, 0, FALSE, DUPLICATE_SAME_ACCESS);
DuplicateHandle (GetCurrentProcess (), m_threadcomm - & gt; M_hThread, GetCurrentProcess (),
& M_htmpdraw, 0, FALSE, DUPLICATE_SAME_ACCESS);
# endif
M_threaddraw - & gt; ResumeThread ();
Sleep (0);//let the draw first run
M_threadcomm - & gt; ResumeThread ();
}
The else
{
While (m_threadcomm - & gt; ResumeThread ()!=0);
}
}
The else
{
GetDlgItem (IDC_BUTTON1) - & gt; SetWindowText (" run ");
While (m_threadcomm - & gt; SuspendThread ()==0);
}
Stop=! Stop;
}

CodePudding user response:

How to write process thread?

CodePudding user response:


I see, you should be in the thread function code is

while(! M_bExit)
{
.
}

So that we can terminate the thread, no problem

CodePudding user response:

 
/*
Extract the exit code for the CWinThread object need extra steps. By default, CWinThread thread terminates,
Thread object is deleted. That is to say can't access m_hThread data members, because the CWinThread object no longer exists.
To this end, want to use one of the following two methods:
Set m_bAutoDelete member to FALSE data.
CWinThread object. After the end of this thread can access m_hThread. Use this method to their destruction
CWinThread object, the framework will not automatically delete it. This is the way to promote.
Or:
Immediately after you create a thread to handle to the thread preserved. The replication m_hThread (use: : DuplicateHandle) to another variable
And through the variable to access. So be deleted automatically at the termination of the object, but you can get the thread to exit.
Note before copying the bureau to handle threads cannot quit. Security approach is to create a thread CREATE_SUSPENDED first, and then ResumeThread.
*/

Void CloseThread ()
{
If ((m_threaddraw==0) & amp; & (m_threadcomm==0)) return;
//
If # 1//if must auto delete, to the as there are resources have to be released
G_bExit=TRUE;
While (m_threadcomm - & gt; ResumeThread ()!=0);
//
DWORD ret=WaitForSingleObject (m_threadcomm - & gt; M_hThread, 1000);//INFINITE);
If (WAIT_TIMEOUT==ret) AfxMessageBox (" WAIT_TIMEOUT comm ");
Ret=WaitForSingleObject (m_threaddraw - & gt; M_hThread, 1000);//INFINITE);
If (WAIT_TIMEOUT==ret) AfxMessageBox (" WAIT_TIMEOUT draw ");
//"m_hThread signaled" does not mean the thread is exits.
# ifdef CHECK_EXIT_CODE
DWORD ex;
Do
{
if(! GetExitCodeThread (m_htmpcomm, & amp; The ex))
{//kill the thread
If (m_threadcomm) m_threadcomm - & gt; The Delete ();
break;
}
} while (ex==STILL_ACTIVE);
DWORD ex1.
Do
{
if(! GetExitCodeThread (m_htmpdraw, & amp; Ex1))
{//kill the thread
If (m_threaddraw) m_threaddraw - & gt; The Delete ();
break;
}
} while (ex1==STILL_ACTIVE);
//BOOL rt=CloseHandle (m_htmpcomm);
//rt=CloseHandle (m_htmpdraw);
AfxMessageBox (" Threads Exited with 0 ");
# endif
//
# else//if can terminate, belong to kill the threads.
If (m_threadcomm) m_threadcomm - & gt; The Delete ();
If (m_threaddraw) m_threaddraw - & gt; The Delete ();
# endif
}

CodePudding user response:

There should be no closure
  • Related