有关在一个线程中立即终止另一个线程的最好办法?

dengdun 2000-02-17 11:24:00
在主线程中如何立即终止工作线程,工作线程中有几条语句十分费时,如连接远程服务器,当网络不通畅时连接时间可能更长。需要能立即终止线程
...全文
762 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
orochi 2000-02-25
  • 打赏
  • 举报
回复
另外是否可以考虑重载OpenURL()函数。
orochi 2000-02-25
  • 打赏
  • 举报
回复
如果是工作线程,我觉得是否可以尽量小的切割工作模块。因为工作线程的终止可以只是简单的返回0,但是如果用到了费时的函数的话是个麻烦事情。
dengdun 2000-02-25
  • 打赏
  • 举报
回复
看来是真的无人解决这个问题了,我也只好给各位送分了,分数不多,还望各位笑纳。
fbq 2000-02-25
  • 打赏
  • 举报
回复
我不得不用中文再说一次:
我认为你不可能完美的解决这个问题!
因为首先你提出的要求就有问题(或者说不切合实际)。假如你杀死的线程正在写一个文件,你杀死线程后还要求文件完整,可能吗?
你既然TIME-OUT要设置30秒,你又要在不到30秒的时间杀了线程,你自己不觉得矛盾吗?
dengdun 2000-02-22
  • 打赏
  • 举报
回复
可是有时候网络状态不好的情况下,Time-Ount设置的太小将会无法连接远程服务器。
fbq 2000-02-22
  • 打赏
  • 举报
回复
你把TIME-OUT设置小些不就行了?
dengdun 2000-02-21
  • 打赏
  • 举报
回复
难道就没人有更好的办法吗?
dengdun 2000-02-21
  • 打赏
  • 举报
回复
请问jing,CSlot和CSvc是什么?
dengdun 2000-02-21
  • 打赏
  • 举报
回复
谢谢fbq的回答,因为我用到了OpenURL()这个函数,当执行这条语句时在网络状态好的情况下一般需要3-5秒钟,如果是在网络状态不好的时候时间会更长。
我已经设置了Time-Out的时间为30秒,所以在网络不好时就会有30秒的时间系统是在执行这一条语句,我希望当系统正在执行该条语句的时候线程也能够立即终止,不知道有没有好办法?
谢谢大家。
dengdun 2000-02-21
  • 打赏
  • 举报
回复
谢谢fbq的回答,因为我用到了OpenURL()这个函数,当执行这条语句时在网络状态好的情况下一般需要3-5秒钟,如果是在网络状态不好的时候时间会更长。
我已经设置了Time-Out的时间为30秒,所以在网络不好时就有30秒的时间系统是在执行这一条语句,我希望当系统正在执行该条语句的时候线程也能够立即终止,不知道有没有好办法?
谢谢大家。
jing 2000-02-21
  • 打赏
  • 举报
回复
用CSlot行吗?
....
CSlot *pSlot = new CSlot(pSvc, newsockfd, id++);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, pSlot, 0, &ul);
....
CSlot::CSlot(CSvc *pSvc, SOCKET sockfd, int id)
{
char buf[80], num[17];

m_id = id;
m_sockfd = sockfd;
pSvc->AddSlot(this);
CSlot *pSlot = pSvc->m_pHead;
strcpy(buf, "User #");
strcat(buf, itoa(m_id, num, 10));
strcat(buf, " has arrived.");
pSvc->SendAll(buf,this);

}

CSlot::~CSlot()
{
char buf[80], num[17];

CSlot *pSlot = m_pSvc->m_pHead;
strcpy(buf, "User #");
strcat(buf, itoa(m_id, num, 10));
strcat(buf, " has just left.");
m_pSvc->SendAll(buf,this);
m_pSvc->RemoveSlot(this);
closesocket(m_sockfd);
}
jing 2000-02-21
  • 打赏
  • 举报
回复
用CSlot行吗?
CSlot::CSlot(CSvc *pSvc, SOCKET sockfd, int id)
{
char buf[80], num[17];

m_id = id;
m_sockfd = sockfd;
pSvc->AddSlot(this);
CSlot *pSlot = pSvc->m_pHead;
strcpy(buf, "User #");
strcat(buf, itoa(m_id, num, 10));
strcat(buf, " has arrived.");
pSvc->SendAll(buf,this);

}

CSlot::~CSlot()
{
char buf[80], num[17];

CSlot *pSlot = m_pSvc->m_pHead;
strcpy(buf, "User #");
strcat(buf, itoa(m_id, num, 10));
strcat(buf, " has just left.");
m_pSvc->SendAll(buf,this);
m_pSvc->RemoveSlot(this);
closesocket(m_sockfd);
}
fbq 2000-02-21
  • 打赏
  • 举报
回复
PostThreadMessages is impossible!because when the thread is busy ,thread hardly hang up and can not receive message.
I think you can not solution the problem perfectly,because at first it is your wrong!You said "工作线程中有几条语句十分费时",what is "十分费时"?If routines waste too much time ,it should support a functionality "time-out",and it should return "time-out" error !
if "十分费时" is not "time-out",you have to kill thread directly.
I am sorry that I can not input chinese in this computer.
WHQ 2000-02-17
  • 打赏
  • 举报
回复
如果是界面线程,则可以给它发消息PostQuitMessage
kxy 2000-02-17
  • 打赏
  • 举报
回复
)[美] Jeffrey Richter
Windows NT 高级编程技术.
中提到:
当线程是由于自然原因或自杀而死亡时,该线程中的堆栈将被撤销.
但是该线程如果是他杀(调用TeriminateThread),windows在拥有该
线程的进程退出之前不会撤销该堆栈.
另外,在线程终止时,windows将通知所有挂接在这个拥有终止线程的
进程上的Dlls,告诉它们线程将被终止.但是他杀的不会.例如,在线程
脱离与某个Dll的挂接时,该Dll可能要往磁盘上刷新数据,如果dll
没有得到通知,.....
同样TerminateProcess也是一样的.
由此,不到万不得已,不要使用TerminteThred和TerimnateProcess!!!!!
dengdun 2000-02-17
  • 打赏
  • 举报
回复
不用TerminateThread,因为用TerminateThread()会出现内存泄漏,
netmare 2000-02-17
  • 打赏
  • 举报
回复
BOOL TerminateThread(
HANDLE hThread, // handle to the thread
DWORD dwExitCode // exit code for the thread
);
olo 2000-02-17
  • 打赏
  • 举报
回复
PostThreadMessage行不行?
kola 2000-02-17
  • 打赏
  • 举报
回复
可以使用消息,并在被终止线程接到终止的消息后,进行资源释放,这样可以避免内存
泄露!

15,471

社区成员

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

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