关于C++中创建线程后,挂起线程的有关问题
关于C++中创建线程后,挂起线程的问题。
class CAotuTestDlg : public CDialog
{
// Construction
public:
CWinThread *pThread;
在start按钮下 创建子线程
pThread = AfxBeginThread(MyTestThread,this,0,0,0,NULL);
在暂停按钮下,挂起或继续线程
PauseFlag=!PauseFlag;
if (PauseFlag==0)
{
pThread->ResumeThread();//pThread为true,恢复线程
SetDlgItemText(IDC_SUSPEND,"暂停测试");
m_information.InsertString(-1,"继续测试...");
}
else if (PauseFlag==1)
{
pThread->SuspendThread();//pThread为false,挂起线程
SetDlgItemText(IDC_SUSPEND,"继续测试");
m_information.InsertString(-1,"暂停测试...");
}
为什么一到挂起或继续线程这里就说内存泄露,请问各位是什么原因啊。难道是 CWinThread *pThread; 这里定义的有问题。
------解决思路----------------------
DWORD Value = pThread->ResumeThread();//If the return value is one, the thread was suspended, but is now restarted. Any return value greater than one means the thread remains suspended.
在看挂起线程的返回值:
DWORD Value = pThread->SuspendThread();//The thread’s previous suspend count if successful; 0xFFFFFFFF otherwise
另外在析构函数里或者结束线程里注意CloseHandle(pThread)
附:
Use AfxBeginThread to create a thread object and execute it in one step. Use CreateThread if you want to reuse the thread object between successive creation and termination of thread executions
class CAotuTestDlg : public CDialog
{
// Construction
public:
CWinThread *pThread;
在start按钮下 创建子线程
pThread = AfxBeginThread(MyTestThread,this,0,0,0,NULL);
在暂停按钮下,挂起或继续线程
PauseFlag=!PauseFlag;
if (PauseFlag==0)
{
pThread->ResumeThread();//pThread为true,恢复线程
SetDlgItemText(IDC_SUSPEND,"暂停测试");
m_information.InsertString(-1,"继续测试...");
}
else if (PauseFlag==1)
{
pThread->SuspendThread();//pThread为false,挂起线程
SetDlgItemText(IDC_SUSPEND,"继续测试");
m_information.InsertString(-1,"暂停测试...");
}
为什么一到挂起或继续线程这里就说内存泄露,请问各位是什么原因啊。难道是 CWinThread *pThread; 这里定义的有问题。
------解决思路----------------------
DWORD Value = pThread->ResumeThread();//If the return value is one, the thread was suspended, but is now restarted. Any return value greater than one means the thread remains suspended.
在看挂起线程的返回值:
DWORD Value = pThread->SuspendThread();//The thread’s previous suspend count if successful; 0xFFFFFFFF otherwise
另外在析构函数里或者结束线程里注意CloseHandle(pThread)
附:
Use AfxBeginThread to create a thread object and execute it in one step. Use CreateThread if you want to reuse the thread object between successive creation and termination of thread executions