判断线程结束

glovel0425 2011-12-07 12:02:10
void Func()
{
m_hDetailThread = AfxBeginThread(_ThreadContactDetail,this,THREAD_PRIORITY_NORMAL);
//判断线程结束。结束后要处理一些事情。
.............. //代码如何编写
//判断已经结束后
OtherFunc();
}

请问这个要如何判断其结束。最好有代码。
...全文
179 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yayafu 2011-12-07
  • 打赏
  • 举报
回复
在return 0前发事件
CppCoder 2011-12-07
  • 打赏
  • 举报
回复
1楼正解
glovel0425 2011-12-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ouyh12345 的回复:]

线程结束后,发个事件
或者WaitForSingleObject
[/Quote]
UINT _ThreadContactDetail(LPVOID pParam)
{
CDlgContactInfo * pdlg = (CDlgContactInfo *) pParam;
pdlg->ThreadContactDetail();

return 0;
}
在哪儿里 发事件? 我现在就是要判断线程是否结束,要知道结束了,也就没疑问了。
bellusvita 2011-12-07
  • 打赏
  • 举报
回复
// 阻塞直到线程结束.
while(WAIT_OBJECT_0 != MsgWaitForMultipleObjects(1, &m_hDetailThread, FALSE, INFINITE, QS_ALLINPUT))
{
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
ouyh12345 2011-12-07
  • 打赏
  • 举报
回复
线程结束后,发个事件
或者WaitForSingleObject
glovel0425 2011-12-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xiaoyuetian 的回复:]

楼上的代码有问题哈

WaitForSingleObject(hThread->m_hObject, INFINITE);

可以哈
[/Quote]我也知道该用这个方法,但是具体使用结合代码就不会写了= =
glovel0425 2011-12-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 visualeleven 的回复:]

C/C++ code
#include "stdafx.h"

#include <iostream>
using namespace std;

UINT __cdecl ThreadProc(LPVOID lParam)
{
cout<<"Work thread start!!!"<<endl;
for(int i=0; i<10; i++)
{
c……
[/Quote]你的那个线程中只是做输出吗?
glovel0425 2011-12-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bellusvita 的回复:]

// 阻塞直到线程结束.
while(WAIT_OBJECT_0 != MsgWaitForMultipleObjects(1, &m_hDetailThread, FALSE, INFINITE, QS_ALLINPUT))
{
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateM……
[/Quote]while条件语句能懂,但是if中的条件还有if中的内容能解释一下吗,对这里不熟 谢谢
xiaoyuetian 2011-12-07
  • 打赏
  • 举报
回复
楼上的代码有问题哈

WaitForSingleObject(hThread->m_hObject, INFINITE);

可以哈
Eleven 2011-12-07
  • 打赏
  • 举报
回复
#include "stdafx.h"

#include <iostream>
using namespace std;

UINT __cdecl ThreadProc(LPVOID lParam)
{
cout<<"Work thread start!!!"<<endl;
for(int i=0; i<10; i++)
{
cout<<"Thread cout: "<<i<<endl;
Sleep(500);
}
cout<<"Work thread end!!!"<<endl;
return 0;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
CWinThread* pThread = AfxBeginThread(ThreadProc, NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
pThread->m_bAutoDelete = FALSE;
HANDLE hThread = pThread->m_hThread;
pThread->ResumeThread();

WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
delete pThread;
pThread = NULL;

cout<<"main thread end!"<<endl;
return 0;
}

15,471

社区成员

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

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