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

    Dealing with WM_DESTROY while terminating a thread

    Hello,
    I have a small thread that I need to terminate when I send WM_CLOSE to it . I am following the MSDN directives to deal with this problem , that is I set the wnd procedure of the thread like this:

    WndProc :

    case WM_CLOSE: DestroyWindow(hWnd);
    break;
    case WM_DESTROY : PostQuitmessage(0);
    return 0;

    I expect the DestroyWindow(hWnd) to send WM_DESTROY to the same thread, which I manage by calling the PostQuitmessage(0) which should cause the message loop of the thread 's window to exit, that is the thread to terminate.

    but what I see is, quite surprisingly, the wnd proc receives the WM_DESTROY before (and not after, as I expected) the DestroyWindow(hWnd) is called.
    I am 100% sure there is no such thing as SendMessage(hWnd, WM_DESTROY,...) nor PostMessage(hWnd, WM_DESTROY, ..) around the application.
    Nor is there any overriding of window handles anywere (I log any handle before using it and I never detected any undue variation of such handles).
    How is it possible ?
    What's even stranger is : I know, because I log almost everything, that I correctly get to DestroyWindow(hWnd) (after receiving WM_CLOSE) and that this function call succeeds, that is it returns nonzero. But this always happens a bunch of nanoseconds after receiving that misterious WM_DESTROY. After calling DestroyWindow(), no more WM_DESTROY seems to be sent....

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dealing with WM_DESTROY while terminating a thread

    Kind of wondering why you are creating a thread containing a window as generally all the UI related stuff is done in the main ui thread.

  3. #3
    Join Date
    Aug 2012
    Posts
    6

    Re: Dealing with WM_DESTROY while terminating a thread

    Because I am not just doing UI related stuff.... I have a worker thread that works behind the scenes and the UI shows some counters and other dynamic info about this work.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Dealing with WM_DESTROY while terminating a thread

    Quote Originally Posted by towmtdj View Post
    Because I am not just doing UI related stuff.... I have a worker thread that works behind the scenes and the UI shows some counters and other dynamic info about this work.
    My question is why the worker thread needs to have a message pump and a window. Generally, worker threads don't have windows.

  5. #5
    Join Date
    May 2013
    Posts
    1

    Re: Dealing with WM_DESTROY while terminating a thread

    Most Internet users are bargain hunters. As you search for discount codes and deals, you may inadvertently stumble across a virus.

Tags for this Thread

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