CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Posts
    10

    PostMessage in a thread

    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!


  2. #2
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: PostMessage in a thread


    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




  3. #3

    Re: PostMessage in a thread

    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.



  4. #4
    Join Date
    Apr 1999
    Posts
    10

    Re: PostMessage in a thread

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured