工作线程操作主界面控件引起死锁及解决
2014-07-07 13:04
295 查看
问题描述:
在监控程序中,设计一监控循环。
头文件 .h
HANDLE m_hEventExit;
CWinThread* m_pThread;
构造函数中,创建该事件
m_hEventExit=CreateEvent(NULL, // 安全
TRUE, // 手动
FALSE, // 初始化为非信号模式
_T("Exit Event") // 线程名称
);
在 OnButtonThreadStart() 中
{
if(!m_pThread)
{
ResetEvent(m_hEventExit);
m_ pThread = AfxBeginThread(MonitorThreadFunc, this);
}
}
MonitorThreadFunc 中需要修改主界面中的控件。
这时候如果在 OnButtonThreadStop ()中
{
SetEvent(m_hEventExit);
if(m_ pThread!= NULL)
{
TRACE0("The thread is still running.\n");
WaitForSingleObject(m_ pThread ->m_hThread, -1);
delete m_ pThread;
m_ pThread = NULL;
}
}
其中 Wait 行使主界面进入等待状态,如果这时候工作线程执行完了,可以顺利退出,如果线程此时正在更新界面控件,就会陷入死锁。
解决方法:
使用 WaitThreadWithHandleMsg 函数,可以在等待线程结束的同时响应消息。
为了使用方便,将该函数封装了一下,使用的时候只需要调用一下。
int WINAPI WaitThreadWithHandleMsg(HANDLE hEventThread)
{
HRESULT hResult = S_FALSE;
BOOL bWait = TRUE;
while (bWait)
{
DWORD dwEvt = MsgWaitForMultipleObjects( 1 , & hEventThread,FALSE,INFINITE,QS_ALLINPUT);
switch (dwEvt)
{
case WAIT_OBJECT_0:
bWait =
false ;
hResult = TRUE;
break ;
case WAIT_OBJECT_0 + 1 :
{
MSG msg;
while (::PeekMessage( & msg, NULL, 0 , 0 , PM_NOREMOVE))
{
if (WM_CLOSE == msg.message || WM_QUIT == msg.message)
{
bWait =
false ;
break ;
}
else
{
PeekMessage( & msg, NULL, 0 , 0 , PM_REMOVE);
TranslateMessage( & msg);
DispatchMessage( & msg);
}
}
break ;
}
default :
// WAIT_TIMEOUT WAIT_FAILED
bWait = false ;
hResult = FALSE;
break ;
}
}
//
end while
return hResult;
}
From:http://www.cppblog.com/sleepwom/archive/2009/02/13/73688.html
在监控程序中,设计一监控循环。
头文件 .h
HANDLE m_hEventExit;
CWinThread* m_pThread;
构造函数中,创建该事件
m_hEventExit=CreateEvent(NULL, // 安全
TRUE, // 手动
FALSE, // 初始化为非信号模式
_T("Exit Event") // 线程名称
);
在 OnButtonThreadStart() 中
{
if(!m_pThread)
{
ResetEvent(m_hEventExit);
m_ pThread = AfxBeginThread(MonitorThreadFunc, this);
}
}
MonitorThreadFunc 中需要修改主界面中的控件。
这时候如果在 OnButtonThreadStop ()中
{
SetEvent(m_hEventExit);
if(m_ pThread!= NULL)
{
TRACE0("The thread is still running.\n");
WaitForSingleObject(m_ pThread ->m_hThread, -1);
delete m_ pThread;
m_ pThread = NULL;
}
}
其中 Wait 行使主界面进入等待状态,如果这时候工作线程执行完了,可以顺利退出,如果线程此时正在更新界面控件,就会陷入死锁。
解决方法:
使用 WaitThreadWithHandleMsg 函数,可以在等待线程结束的同时响应消息。
为了使用方便,将该函数封装了一下,使用的时候只需要调用一下。
int WINAPI WaitThreadWithHandleMsg(HANDLE hEventThread)
{
HRESULT hResult = S_FALSE;
BOOL bWait = TRUE;
while (bWait)
{
DWORD dwEvt = MsgWaitForMultipleObjects( 1 , & hEventThread,FALSE,INFINITE,QS_ALLINPUT);
switch (dwEvt)
{
case WAIT_OBJECT_0:
bWait =
false ;
hResult = TRUE;
break ;
case WAIT_OBJECT_0 + 1 :
{
MSG msg;
while (::PeekMessage( & msg, NULL, 0 , 0 , PM_NOREMOVE))
{
if (WM_CLOSE == msg.message || WM_QUIT == msg.message)
{
bWait =
false ;
break ;
}
else
{
PeekMessage( & msg, NULL, 0 , 0 , PM_REMOVE);
TranslateMessage( & msg);
DispatchMessage( & msg);
}
}
break ;
}
default :
// WAIT_TIMEOUT WAIT_FAILED
bWait = false ;
hResult = FALSE;
break ;
}
}
//
end while
return hResult;
}
From:http://www.cppblog.com/sleepwom/archive/2009/02/13/73688.html
相关文章推荐
- 工作线程操作主界面控件引起死锁及解决
- 工作线程操作主界面控件引起死锁及解决
- 教你如何解决“线程间操作无效: 从不是创建控件的线程访问它”
- 解决多线程操作控件时可能出现的异常:“在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级”
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- c# 线程间操作无效: 从不是创建控件“”的线程访问它,用托管来解决
- 小结“线程间操作无效: 从不是创建控件的线程访问它” 错误的解决方法
- 如何解决“线程间操作无效: 从不是创建控件的线程访问它”
- BackGroundWorker解决“线程间操作无效: 从不是创建控件的线程访问它”
- 解决多线程操作控件时可能出现的异常:“在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级”
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法!
- DotNet2.0不允许线程互操作各控件的解决方法。
- “线程间操作无效:从不是创建控件“XX”的线程访问它”的解决方法
- 线程间操作无效: 从不是创建控件“”的线程访问它~~~的解决方法~
- 引起线程死锁的一种情况及解决方法
- 教你如何解决“线程间操作无效: 从不是创建控件的线程访问它”