怎样强制结束这些任务.....

ssfang 2001-11-29 10:32:43
我有一个小程序,内有三个各自独立运行的线程:
1.下载文件
2.压缩文件
3.拷贝文件
我想不管其中的哪一个在运行,只要有线程存在的时候,我该怎样强制结束这些任务呢?
请各位大虾指点一二。
...全文
177 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
supervar 2001-11-29
  • 打赏
  • 举报
回复
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.
ssfang 2001-11-29
  • 打赏
  • 举报
回复
msdn 我这里有!
gloom 2001-11-29
  • 打赏
  • 举报
回复
MyThread->Handle;

或一个未经尝试的API
HANDLE GetCurrentThread()
whitelion 2001-11-29
  • 打赏
  • 举报
回复
v
ssfang 2001-11-29
  • 打赏
  • 举报
回复
gloom(《一丈丝纶一寸钩,一人独占一江秋》) 
对于一个被封锁了的线程该怎样去得到HANDLE hThread的值呢,
gloom 2001-11-29
  • 打赏
  • 举报
回复
或者用API
BOOL TerminateThread(

HANDLE hThread, // handle to the thread
DWORD dwExitCode // exit code for the thread
);这个应该可以了吧

The TerminateThread function terminates a thread.

BOOL TerminateThread(

HANDLE hThread, // handle to the thread
DWORD dwExitCode // exit code for the thread
);


Parameters

hThread

Identifies the thread to terminate.
Windows NT: The handle must have THREAD_TERMINATE access. For more information, see Thread Objects.

dwExitCode

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.
gloom 2001-11-29
  • 打赏
  • 举报
回复
一个结束进程的方法,没试过线程
SendMessafe(YouHandle,WM_CLOSE,0,0);
icaohong 2001-11-29
  • 打赏
  • 举报
回复
TerminateThread()
不是吗?

关注
ssfang 2001-11-29
  • 打赏
  • 举报
回复
这些线程是已经封装了的,用SendMessage()能实现吗?
781014 2001-11-29
  • 打赏
  • 举报
回复
关注。
icaohong 2001-11-29
  • 打赏
  • 举报
回复
TerminateThread()
不是吗?

关注
gloom 2001-11-29
  • 打赏
  • 举报
回复
可以挂起这个线程么?
紫去秋来 2001-11-29
  • 打赏
  • 举报
回复
要结束一个线程,至少要知道这个线程的ID,你的线程有创建窗体吗?如果有,则可以用 GetWindowsThreadProcessId()

13,824

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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