how to end the ui thread
I create a event in the head file
static CEvent Mstop(FALSE,TRUE,NULL,NULL);
//And in the main thread I Create a UI thread
CSendThread* m_pSThread;
Mstop.SetEvent();
m_pSThread=new CSendThread(this);
m_pSThread->CreateThread();
//But in the creadted thread it still cannot work
if(WaitForSingleObject(Mstop.m_hObject,5)!=WAIT_OBJECT_0)
{
PostThreadMessage(WM_QUIT,-1,0);
}
else
{
// do some work
}
i also try to use a static ,
the value of that in the create thread can not be changed by the main thread,
I don't know why
please help me!!!!
[721 byte] By [
newman1982] at [2007-11-19 7:11:51]

# 1 Re: how to end the ui thread
The code you written, contains PostThreadMessage with only three parameters (the first parameter seems to be absent).
I think that the first parameter is the thread identifier of the UI thread.
I don't really understand, why you use WaitForSingleObject, perhaps the second parameter of WaitForSingleObject is 5000 (time in milli-seconds), and not 5.
I have searched PostThreadMessage with google and find that:
Dont Use the Win32 API PostThreadMessage() to Post Messages to UI Threads
PostThreadMessage is a Win32 API used to post messages to threads. Usually, the message posted is a standard windows message with the window handle set to NULL.
When PostThreadMessage is used to post messages to a thread that has created a window, it is very likely that the posted messages will be lost. This is because UI threads are not always run by the primary message loop. For example, when a thread is showing a message box, it is running on the message loop supplied by the message box. This secondary message loop does not know how to handle the thread message (since its window handle is NULL) and it will be dropped.
So when posting messages to a UI threads, use PostMessage() instead and post messages to a window owned by that thread. Then the messages wont be lost, even if the thread is running a secondary message loop.