WaitForSingleObject等不到事件

侠雨飞飞 2010-07-23 10:26:42
两个线程A,B;
定义了全局事件
CEvent eventClose;

线程A在退出前
SetEvent(eventClose);
return 0;

线程B里:

WaitForSingleObject(eventClose.m_hObject,INFINITE);
//do what i do

运行时,线程A的确退出了。
但是,线程B却一直还在等。

查了N多资料,不解?

谢谢了~

...全文
338 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
侠雨飞飞 2010-07-29
  • 打赏
  • 举报
回复
晕倒了,真不好意思,犯了经验主义错误;
太相信SendMessage(hwinlc.hwnd_btned ,WM_ENABLE,true,(LPARAM)false);
以为肯定会成功的,由此也判断现场B一直在等。
实际上,event事件收到了;
只是SendMessage(hwinlc.hwnd_btned ,WM_ENABLE,true,(LPARAM)false);没有成功。
发送了三个,static控件收到了
按钮没有收到。
magic7004 2010-07-23
  • 打赏
  • 举报
回复
楼主要么贴代码,要么自己慢慢调试吧
Eleven 2010-07-23
  • 打赏
  • 举报
回复

#include <iostream>
#include <afxmt.h>
using namespace std;

CEvent evt;

UINT __cdecl ThreadStart(LPVOID lParam)
{
for(int i=0; i<10; i++)
{
cout<<"ThreadStart: "<<i<<endl;
Sleep(500);
}

evt.SetEvent();
return 0;
}

UINT __cdecl ThreadClose(LPVOID lParam)
{
WaitForSingleObject(evt.m_hObject, INFINITE);
for(int i=0; i<10; i++)
{
cout<<"ThreadClose: "<<i<<endl;
Sleep(500);
}
return 0;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
AfxBeginThread(ThreadStart, NULL);
CWinThread* pClose = AfxBeginThread(ThreadClose, NULL);

WaitForSingleObject(pClose->m_hThread, INFINITE);

cout<<"---------End----------"<<endl;
return 0;
}
weblai 2010-07-23
  • 打赏
  • 举报
回复
没看出问题,这样试试
HANDLE hThreadfunc=createthread(...Threadfunc...);

::SendMessageA(hwnds->hwnd_static ,WM_SETTEXT,true,(LPARAM)"服务停止中,请稍候......");

把 WaitForSingleObject(eventClose.m_hObject,INFINITE);

改成 WaitForSingleObject(hThreadfunc,INFINITE);

jhony_lee 2010-07-23
  • 打赏
  • 举报
回复
你定义的CEvent eventClose;有问题吧!
侠雨飞飞 2010-07-23
  • 打赏
  • 举报
回复
全局声明:
CEvent eventClose;

线程A:
UINT Threadfunc(LPVOID pParam){


while (1==1) {
if(stopThread) {
SetEvent(eventClose);
return 0;
}
......

}
}






线程B:

//停止发送线程
UINT Threadclose(LPVOID pParam){
stopThread=true;


SHwndclose* hwnds=(SHwndclose*)pParam;


//m_Cinfo.SetWindowTextA("服务停止中,请稍候......");
::SendMessageA(hwnds->hwnd_static ,WM_SETTEXT,true,(LPARAM)"服务停止中,请稍候......");

WaitForSingleObject(eventClose.m_hObject,INFINITE);

...
}


zenghmail 2010-07-23
  • 打赏
  • 举报
回复
CEvent* pEvent = new CEvent(FALSE, FALSE);
如果你写的也是这样,会不会是你线程A结束太快,还没有等到B的WaitForSingleObject(eventClose.m_hObject,INFINITE),就结束了。
zhou1xp 2010-07-23
  • 打赏
  • 举报
回复
逻辑上是对的,你的执行代码有问题
Eleven 2010-07-23
  • 打赏
  • 举报
回复
把你的代码贴出来看看
雪影 2010-07-23
  • 打赏
  • 举报
回复
你说的逻辑没问题,应该是你的代码出问题了
贴代码吧

参考
// The following demonstrates trivial usage of the CEvent class.
// A CEvent object is created and passed as a parameter to another
// thread. The other thread will wait for the event to be signaled
// and then exit

UINT __cdecl MyThreadProc(LPVOID lpParameter)
{
CEvent* pEvent = (CEvent*)(lpParameter);
VERIFY(pEvent != NULL);

// Wait for the event to be signaled
::WaitForSingleObject(pEvent->m_hObject, INFINITE);

// Terminate the thread
::AfxEndThread(0, FALSE);
return 0L;
}

void CEvent_Test()
{
// Create the CEvent object that will be passed to the thread routine
CEvent* pEvent = new CEvent(FALSE, FALSE);

// Create a thread that will wait on the event
CWinThread* pThread;
pThread = ::AfxBeginThread(&MyThreadProc, pEvent, 0, 0, CREATE_SUSPENDED, NULL);
pThread->m_bAutoDelete = FALSE;
pThread->ResumeThread();

// Signal the thread to do the next work item
pEvent->SetEvent();

// Wait for the thread to consume the event and return
::WaitForSingleObject(pThread->m_hThread, INFINITE);
delete pThread;
delete pEvent;
}
crystalbug 2010-07-23
  • 打赏
  • 举报
回复
嗯对,事件对象最好显示值初始化构造,默认构造的习惯很不好,看看这样能不能解决你的问题
来灵 2010-07-23
  • 打赏
  • 举报
回复
这样定义你的Event吧

CEvent eventClose(FALSE, TRUE, NULL, NULL);


侠雨飞飞 2010-07-23
  • 打赏
  • 举报
回复
单步,
线程A的确执行了SetEvent()
也退出了;
线程B一直在等事件变化;
奇怪的是,按理说线程A SetEvent后,
线程B应该能收到,实际上,线程B一直在等。
昨夜无风 2010-07-23
  • 打赏
  • 举报
回复
看看线程B结束了没有!!
侠雨飞飞 2010-07-23
  • 打赏
  • 举报
回复
嗯,确认执行了
A线程是扫描某个表,把数据累加。
停止后,表数据不再变化了。
我在SetEvent(eventClose);
return 0;
直接加调试代码,也能执行。

hzy694358 2010-07-23
  • 打赏
  • 举报
回复
while (1==1) {
if(stopThread) {
SetEvent(eventClose);
return 0;
}
你确定执行了SetEvent(eventClose);?
侠雨飞飞 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 tttyd 的回复:]
是不是::SendMessageA(hwnds->hwnd_static ,WM_SETTEXT,true,(LPARAM)"服务停止中,请稍候......");代码影响了?

删除这行代码试一试
[/Quote]
和这个没有关系,我删除了,还是不行
侠雨飞飞 2010-07-23
  • 打赏
  • 举报
回复
在启动按钮事件里,启动A线程
。。。

LPVOID param;
param=&sHwnd;
workThread=AfxBeginThread(&Threadfunc,param);
//ResetEvent(eventClose);

m_Cbtn_Thread.EnableWindow(false);
m_end.EnableWindow(true);
m_time.SetWindowText (getCurrentTime("%Y-%m-%d %H:%M:%S"));
writeLog("启动服务++++++++++");
。。。

在关闭按钮事件里,启动B线程
//关闭用进程
SHwndclose sHwndclose;
sHwndclose.hwnd_static=m_Cinfo;
sHwndclose.hwnd_btned=m_end;
sHwndclose.hwnd_btnst=m_start;
LPVOID paramc;
paramc=&sHwndclose;
AfxBeginThread(&Threadclose,paramc);
雪影 2010-07-23
  • 打赏
  • 举报
回复
是不是::SendMessageA(hwnds->hwnd_static ,WM_SETTEXT,true,(LPARAM)"服务停止中,请稍候......");代码影响了?

删除这行代码试一试
侠雨飞飞 2010-07-23
  • 打赏
  • 举报
回复
代码我已经贴了,线程A省略部分是纯数据库操作的,而且线程A的确是退出了。
另外我也查了,和VisualEleven最后给出的调用方式基本一样
真是匪夷所思。

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧