请问结束线程的函数怎么用?(参数)有几个线和结束的函数?

解决方案 »

  1.   

    Normal Thread Termination
    For a worker thread, normal thread termination is simple: Exit the controlling function and return a value that signifies the reason for termination. You can use either theAfxEndThread function or a return statement. Typically, 0 signifies successful completion, but that is up to you. For a user-interface thread, the process is just as simple: from within the user-interface thread, call::PostQuitMessage in the Win32 Programmer’s Reference, Volume 4. The only parameter that ::PostQuitMessage takes is the exit code of the thread. As for worker threads, 0 typically signifies successful completion.Premature Thread Termination
    Terminating a thread prematurely is almost as simple: CallAfxEndThread from within the thread. Pass the desired exit code as the only parameter. This stops execution of the thread, deallocates the thread’s stack, detaches all DLLs attached to the thread, and deletes the thread object from memory.AfxEndThread must be called from within the thread to be terminated. If you want to terminate a thread from another thread, you must set up a communication method between the two threads.Retrieving the Exit Code of a Thread
    To get the exit code of either the worker or the user-interface thread, call the::GetExitCodeThread function. For information about this function, see the Win32 Programmer’s Reference, Volume 3. This function takes the handle to the thread (stored in the m_hThread data member of CWinThread objects) and the address of a DWORD.If the thread is still active, ::GetExitCodeThread will place STILL_ACTIVE in the supplied DWORD address; otherwise, the exit code is placed in this address.Retrieving the exit code ofCWinThread objects takes an extra step. By default, when a CWinThread thread terminates, the thread object is deleted. This means you cannot access the m_hThread data member because the CWinThread object no longer exists. To avoid this situation, do one of the following two things: Set the m_bAutoDelete data member to FALSE. This allows the CWinThread object to survive after the thread has been terminated. You can then access the m_hThread data member after the thread has been terminated. If you use this technique, however, you are responsible for destroying the CWinThread object as the framework will not automatically delete it for you. This is the preferred method.
    -or-Store the thread’s handle separately. After the thread is created, copy its m_hThread data member (using ::DuplicateHandle) to another variable and access it through that variable. This way the object is deleted automatically upon termination and you can still find out why the thread terminated. Be careful that the thread does not terminate before you can duplicate the handle. The safest way to do this is to pass CREATE_SUSPENDED toAfxBeginThread, store the handle, and then resume the thread by callingResumeThread. 
    Either method allows you to determine why a CWinThread object terminated. 
      

  2.   

    ExitThread 在线程中使用,一个参数,就是返回值
    TerminateThread 在线程外使用,一个参数,是返回值
      

  3.   

    SinAngel(纹身女孩)说的对,但多不赞成你使用它们.
    ExitThread很差劲,一旦你的线程中有个C++类的实例的话,那么它的析构函数永远不
    会调用.
    TerminateThread 稍好点,但它是异步的,也就是说,它总是立即返回.
    这使得其它线程若访问此线程的话,会出现访问违规. 且DLL也不会收到
    通知,这会阻止你的适当的清除.
      

  4.   

    结束父进程
    ExitThread 
    TerminateThread