有两个自定义函数A和B,当B执行时,A要马上退出,怎么做呢?
解决方案 »
- 文件关联程序 双击文件时用我的程序打开
- 控件标号的和单选框的问题
- 在一个线程里面,用GlobalAlloc函数做一个BUFFER时候抛了一个异常.大家来聊聊
- 如何在一个函数中发送消息,在另一个函数获取它
- 用ExitWindowsEx 定时关机,在win2000porfessional下好用,在win2000server下报错cmd.exe
- 这段代码能正常运行,但当禁用网卡时会出错,但为什么抓不住异常呢?请指教!
- 谁能提供 控制IIS的技术资料(IIS://localhost/w3svc)
- 请问大家如何调试dll?
- 将XML和XSL文件合成独立的html文件
- 使用CreateDIBSection显示bmp文件
- 内存锁定
- 高手帮我解答难题vc++ OpenGL动画
A中
hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
while(1)
{
if(WaitForSingleObject(hEvent,0)!=WAIT_TIMEOUT)
{
//退出
}}B开始时
SetEvent(hEvent);
同步的方法很多,建议看下《Windows核心编程》(第五版)
函数的声明如下:
BOOL TerminateThread( HANDLE hThread, DWORD dwExitCode);
作用:
终止一个线程
参数说明:
HANDLE htread:被终止的线程的句柄
DWORD dwExitCode:退出代码
返回值:
函数执行成功则返回非零值,执行失败返回0。调用getlasterror获得返回的值有问题随时问我。
TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code. DLLs attached to the thread are not notified that the thread is terminating. The system frees the thread's initial stack.Windows Server 2003 and Windows XP/2000: The target thread's initial stack is not freed, causing a resource leak.
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 allocating memory from the heap, the heap lock 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.
TerminateThread用于结束一个线程。同时,目标线程会没有机会执行任何用户模式代码. 和该线程有关的DLLs将不会得到线程退出的通知. 系统将负责释放线程的调用栈.Windows Server 2003 和 Windows XP/2000: 目标线程的调用栈将不会被系统释放,由此引发一个资源泄露.
TerminateThread是一个危险的函数,应该仅当最极端的情况下才被用到. 你只能在确切地清楚目标线程所作所为的情况下调用TerminateThread, 并且这时目标线程代码处于你完全地控制着的情况下. 例如, TerminateThread可能导致以下问题:•如果目标线程拥有关键区,关键区将不会被释放.
•如果目标线程分配了堆内存, 锁定的堆将不会被释放.
•如果调用TerminateThread时目标线程正在执行特定的kernel32调用, 则线程所在的进程内的kernel32就会处于未知状态.
•如果目标线程正在维护着共享dll的全局状态, 该DLL的状态就可能被破坏并影响到该DLL的其它宿主.
线程无法保护自己拒绝TerminateThread结束自己, 担可以控制自己线程句柄的访问权限. 由CreateThread和CreateProcess反回的线程句柄拥有THREAD_TERMINATE访问权, 所以任何控制这句柄的调用者都可以强制结束线程.如果目标线程是进程内的最后一个线程, 线程所在的进程也会终止.线程对象的状态会被置信号,并让任何其它等待自己结束的线程释放等待状态.线程的终止状态码从STILL_ACTIVE变成参数dwExitCode给定的值
强制结束一个线程不一定会将线程对象从系统删除掉,线程对象会在他的最后一个句柄被关闭时被(系统)删除。
goto 是个好东西,不要看不起这个语句啊
但我很反对滥用goto、terminators等。
另外这些goto和terminators会导致析构函数没有机会被执行或错误地被调用。