Click to See Complete Forum and Search --> : PostMessage in a thread


Canada Bass
April 22nd, 1999, 08:44 PM
Hello all, I am trying to close an MFC app after I have met a certain condition. In a thread I have created, I use:


m_pOwner->PostMessage(WM_CLOSE, 0, 0L);



m_pOwner is a pointer to my AppDlg.

I have looked at the example Mutexes(on the VC++ cd) and it does this also. I have tried to create an application that is very close to what Mutexes does. However, if I do this my program crashes when I run it. I have two threads running. If I comment out the first thread(so that it doesn't run) this command works fine, but once I uncomment the lines the second thread generates an error. It may be that I have to use CSingleLock in both threads, I do use it in the second thread. Any help would be appreciated. Thanks!

sally
April 22nd, 1999, 11:45 PM
One solution might be to send a user defined messsage to the app
the app will then close the calling thread and shut itself down

The way to tell the app to die is to use PostThreadMessage()

sally

Sally
April 22nd, 1999, 11:45 PM
One solution might be to send a user defined messsage to the app
the app will then close the calling thread and shut itself down

The way to tell the app to die is to use PostThreadMessage()

sally

Stefan Tchekanov
April 23rd, 1999, 06:01 AM
If the CWnd object is created in one thread you cannot pass this object to another directly. Instead you should pass its window handle and in the other thread create a CWnd object from this HWND value. And after that you can use CWnd object. This is because MFC classes are not thread safe. And while CDialog is derived from CWnd you should use this technique.

Another way to solve the problem is to call ::PostThreadMessage() and to send messages to the main thread which are not window messages. So you should define your own user message, handle it in your CWinThread derived class (CWinApp is derived from CWinThread) and from that handler do whatever you want.

I hope this helps.

Canada Bass
April 23rd, 1999, 11:13 AM
The problem with the PostThreadMessage is that the dialog is Modal. All the App really does is update a file while a program is running, then when the program closes my App needs to close.