主线程先暂停辅助线程再在主线程中
   pThread->PostThreadMessage(WM_QUIT,0,0);
   pThread->ResumeThread();
怎么直接终止?不让辅线程继续执行?

解决方案 »

  1.   

    先转一段话(MSDN上的):
    The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation: Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds. 
    Create an event object, then create the thread. Use the WaitForSingleObject function to wait for the event to be set to the signaled state before calling PostThreadMessage. In the thread to which the message will be posted, call PeekMessage as shown here to force the system to create the message queue. 
    PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
    Set the event, to indicate that the thread is ready to receive posted messages. The thread to which the message is posted retrieves the message by calling the GetMessage or PeekMessage function. The hwnd member of the returned MSG structure is NULL. Messages sent by PostThreadMessage are not associated with a window. Messages that are not associated with a window cannot be dispatched by the DispatchMessage function. Therefore, if the recipient thread is in a modal loop (as used by MessageBox or DialogBox), the messages will be lost. To intercept thread messages while in a modal loop, use a thread-specific hook. The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling. Windows 2000/XP: There is a limit of 10,000 posted messages per message queue. This limit should be sufficiently large. If your application exceeds the limit, it should be redesigned to avoid consuming so many system resources. To adjust this limit, modify the following registry key: HKEY_LOCAL_MACHINE
    SOFTWARE
    Microsoft
    Windows NT
    CurrentVersion
    Windows
    USERPostMessageLimit
    The minimum acceptable value is 4000. Windows 95/98/Me: PostThreadMessageW is supported by the Microsoft® Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems. 就是说
    PostThreadMessage只是发送消息到线程的消息队列中,至于辅助线程退出不退出,还是要看辅助线程的意思,也就是说辅助线程使用PeekMessage收到了退出消息(或者其他消息)才决定退出。所以说要辅助线程退出,必须要跟主线程约定好。在转一段话:
    SuspendThreadIf the function succeeds, execution of the specified thread is suspended and the thread's suspend count is incremented. Suspending a thread causes the thread to stop executing user-mode (application) code.This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.Each thread has a suspend count (with a maximum value of MAXIMUM_SUSPEND_COUNT). If the suspend count is greater than zero, the thread is suspended; otherwise, the thread is not suspended and is eligible for execution. Calling SuspendThread causes the target thread's suspend count to be incremented. Attempting to increment past the maximum suspend count causes an error without incrementing the count.The ResumeThread function decrements the suspend count of a suspended thread
    明白了吗?
      

  2.   

    还有,直接使用TerminateThread不会释放所结束线程的堆栈空间。