如何强制结束一个子线程?

vcshcn 2002-08-25 11:45:01
有一个子线程调用了一个阻塞的函数,现在我想强制结束这个子线程,该用什么办法?
...全文
221 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
snsins 2002-08-26
  • 打赏
  • 举报
回复
这是摘自MSDN,你不知道吗?

BOOL TerminateThread(
HANDLE hThread, // handle to thread
DWORD dwExitCode // exit code
);

这个函数就可以实现强制结束线程的功能
比如

TerminateThread(hThread,0);
其中hThread就是创建线程时候返回的句柄

这个是WINDOWS的API

lingjingqiu 2002-08-25
  • 打赏
  • 举报
回复
不知道是从哪里抄的,用什么ying文,楼上的。
snsins 2002-08-25
  • 打赏
  • 举报
回复
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: 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.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated. DLLs attached to the thread are not notified that the thread is terminating.

TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:

If the target thread owns a critical section, the critical section will not be released.
If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.
A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.

If the target thread is the last thread of a process when this function is called, the thread's process is also terminated.

The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter.

Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.

Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.

See Also
Processes and Threads Overview, Process and Thread Functions, CreateProcess, CreateThread, ExitThread, GetExitCodeThread, OpenThread

Built on Thursday, May 11, 2000

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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