线程跨类无法关闭的问题???

mirro187_ 2013-02-06 01:36:22

MainFrame类里
CWinThread* pThread = NULL;//全局变量


void CMainFrame::OnManageUser()//工具栏的按钮按下消息,弹出对话框并触发一个线程。
{
if(!ShowObj.ShowWindow(SW_SHOW))
{
pThread = AfxBeginThread((AFX_THREADPROC)MyThreadProc,GetSafeHwnd());
// pThreadOut = pThread;
// bflagTime = TRUE;
}
}


UINT WINAPI MyThreadProc(LPVOID pParam)
{

while(1);
}

//对话框类里关闭消息关闭线程,关闭不了。卡住并强制关闭程序。。。。

extern CWinThread* pThread;

void AlarmDlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
DWORD dwECode;
GetExitCodeThread(pThread->m_hThread,&dwECode);
if(dwECode == STILL_ACTIVE)
{
TerminateThread(pThread->m_hThread,0);
CloseHandle(pThread->m_hThread);
pThread->m_hThread = NULL;
}
CDialog::OnClose();
}



::AfxEndThread(0);//随便问下,这种关闭线程的方法怎么连个线程句柄都不需要指定呢????
...全文
88 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
这个娜戒海了 2013-02-06
  • 打赏
  • 举报
回复
马克
看不见的裂痕 2013-02-06
  • 打赏
  • 举报
回复
UINT WINAPI MyThreadProc(LPVOID pParam) { while(1); } 虽说是线程,但是这样把CPU资源都占了。加个Sleep吧。 AfxEndThread指定结束当前线程, 0是退出码
bsnry 2013-02-06
  • 打赏
  • 举报
回复
引用 3 楼 VisualEleven 的回复:
::AfxEndThread(0);//随便问下,这种关闭线程的方法怎么连个线程句柄都不需要指定呢???? -------------- AfxEndThread()函数是结束当前执行的线程的,不是关闭其它线程的,所以不需要线程句柄。 Call this function to terminate the currently executing thread. ……
这个会导致内存泄露的,比如线程函数里只有2句代码 cstring str="hello"; Afxendthread(0); 析构函数不执行,内存泄露 所以不要使用
Eleven 2013-02-06
  • 打赏
  • 举报
回复
::AfxEndThread(0);//随便问下,这种关闭线程的方法怎么连个线程句柄都不需要指定呢???? -------------- AfxEndThread()函数是结束当前执行的线程的,不是关闭其它线程的,所以不需要线程句柄。 Call this function to terminate the currently executing thread.
bsnry 2013-02-06
  • 打赏
  • 举报
回复
DWORD dwECode; GetExitCodeThread(pThread->m_hThread,&dwECode); if(dwECode == STILL_ACTIVE) { TerminateThread(pThread->m_hThread,0); CloseHandle(pThread->m_hThread); pThread->m_hThread = NULL; } 这个也是错误的, 你需要反复的while循环,判断线程是否活着 while(1) { getexitcode if(返回码==still_active) { 关闭线程 } else break; } 因为 你执行关闭线程函数后,不是说立即就关闭了。 一家之言,不对的,请之处,并附代码验证
bsnry 2013-02-06
  • 打赏
  • 举报
回复
引用 楼主 mirro187_ 的回复:
C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738MainFrame类里CWinThread* pThread = NULL;//全局变量 void CMainFrame::OnManageUser()//工具栏的按钮按下消息,弹出对话框并触发一个线程。{ ……
while里加一句: sleep(20); 试试

15,473

社区成员

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

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