退出线程的问题!

zhucde 2003-08-18 06:50:31
定义了一个线程类,名为CMyThread,

调用时用如下语句:.

CRuntimeClass* prt;
prt = RUNTIME_CLASS(CMyThread);
AfxBeginThread(prt);

这种方法创建的线程如何终止?
...全文
63 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhucde 2003-08-19
  • 打赏
  • 举报
回复
感谢以上各位参与,我已经想出办法解决了,
大家忘了一个函数:AfxGetThread(),
如果谁有兴趣知道,可以和我联系,大家一起学习,谢谢各位的参与
hoopp 2003-08-19
  • 打赏
  • 举报
回复
最好不要直接强行中止线程,而采用发消息的方式或者事件通知的方式。
具体在运行线程中waitforXXXX,而在调用线程中SetEvent
要退出运行线程只要在线程函数中Return就可以了。
flinming 2003-08-19
  • 打赏
  • 举报
回复
HANDLE m_hThread; //线程句柄

if (TerminateThread(XTApp->Thread.m_hThread, 4))
{
DWORD dwExitCode =0;
while(TRUE)
{
GetExitCodeThread(XTApp->Thread.m_hThread, &dwExitCode);
if (dwExitCode != STILL_ACTIVE)
break;
Sleep(10);
}
CloseHandle(XTApp->Thread.m_hThread);
}
zhucde 2003-08-19
  • 打赏
  • 举报
回复
感谢以上各位的参与,
以上方法我知道,
关键是使用线程类的方法调用的线程怎样去终止?
zhucde 2003-08-19
  • 打赏
  • 举报
回复
感谢以上各位的参与,
以上方法我知道,
关键是使用线程类的方法调用的线程怎样去终止?
joinrry 2003-08-19
  • 打赏
  • 举报
回复
来迟了?
Zark 2003-08-18
  • 打赏
  • 举报
回复
在线程内部可用AfxEndThread()或是直接return 0;
在线程外部可以
1. 在CWinThread子类中设计一个方法,比如StopRun(),在其中使CWinThread停下来.或
2. 直接在外部调用::TerminateThread(),其中的HANDLE是CWinThread对象中的成员m_hThread.
zhouyong0371 2003-08-18
  • 打赏
  • 举报
回复
创建线程的时候必须保存线程的句柄,要结束的时候:
TerminateThread(hThread, 0);
zhouyong0371 2003-08-18
  • 打赏
  • 举报
回复
TerminateThread
The TerminateThread function terminates a thread.

BOOL TerminateThread(
HANDLE hThread, // handle to thread
DWORD dwExitCode // exit code
);
Parameters
hThread
[in/out] Handle to the thread to terminate.
Windows NT/2000 or later: The handle must have THREAD_TERMINATE access. For more information, see Thread Security and Access Rights.

dwExitCode
[in] Specifies the exit code for the thread. Use the GetExitCodeThread function to retrieve a thread's exit value.

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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