WaitForSingleObject Question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: WaitForSingleObject Question

  1. #1
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175

    WaitForSingleObject Question

    Does anyone know if you can use WaitForSingleObject on a window? I have a handle to the window (HWND) and I want to know when the user closes the window. WaitForSingleObject seems to only work on processes and not windows.

    Your help is greatly appreciated.

    Thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011
    According to MSDN:
    The WaitForSingleObject function can wait for the following objects:

    Change notification
    Console input
    Event
    Job
    Mutex
    Process
    Semaphore
    Thread
    Waitable timer
    There's no "window handle" in this list
    Sorry!

  3. #3
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175
    Thanks - I think I will use the following code instead:

    BOOL WaitForWindowExit()
    {
    Sleep(5000);
    HWND WFWindow = FindWindow("IEFrame", "My Window - Microsoft Internet Explorer");

    while(WFWindow != NULL)
    {
    Sleep(3000);
    WFWindow = FindWindow("IEFrame", "My Window - Microsoft Internet Explorer");
    }
    return true;
    }

  4. #4
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,915
    You cannot use window handle but you can use thread or process handle.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Jul 2003
    Location
    Belgium
    Posts
    19

    Lightbulb Catch the WM_CLOSE

    What you should do is catch the WM_CLOSE message from the window, and then send a (user-defined) message (or call a function) to whoever wants to know.
    "GOTO's make your co-workers laugh at you"

  6. #6
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,915
    The assumption is that drm15 wants to know when some other window is closed - window created by another application not programmed by drm15.

    Therefore in order to catch WM_CLOSE message global hook would have to be set and that is not necessary for that task.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175
    The last post is correct, I don't know how to listen for the WM_CLOSE when I don't have the process. The reason I cannot use the waitForSingleObject on the process id is because the process id does not go away when the applications window is closed. I think this is a feature of Java running on the MVM? The problem is that it has ports open and if the process is not stopped then the ports are not closed. This is why I need to listen for the window to close and not for the process to terminate.

    Thanks to everyone for your help!

  8. #8
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Are you having the handle to the window you want to spy on ?

  9. #9
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,915
    Originally posted by drm15
    I have a handle to the window (HWND) and I want to know when the user closes the window.
    Again, I think that:
    Originally posted by me
    You cannot use window handle but you can use thread or process handle.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  10. #10
    Join Date
    Jan 2004
    Posts
    56
    U can also subclass that window then intercept its WM_DESTROY message.
    Trust urself!

  11. #11
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994
    You have got a window handle, and want to use WaitForSingleObject?
    Try this:
    Code:
    DWORD dwProcessID = 0;
    DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
    if(0 != dwThreadID)
    {
       HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
       if(NULL != hProcess)
       {
          ::WaitForSingleObject(hProcess, INFINITE);
          AfxMessageBox(_T("Terminated"));
       }
    }
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  12. #12
    Originally posted by sephiroth2m
    U can also subclass that window then intercept its WM_DESTROY message.
    Only the window is created by your current process.
    Best Api Monitor tool.
    Trace the target program automatically and monitor the parameters of all API and COM interfaces.

    Auto Debug for Windows 4.0
    Auto Debug for .Net
    http://www.autodebug.com/

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011
    DWORD dwProcessID = 0;
    DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
    if(0 != dwThreadID)
    {
    HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
    if(NULL != hProcess)
    {
    ::WaitForSingleObject(hProcess, INFINITE);
    AfxMessageBox(_T("Terminated"));
    }
    }
    And don't forget to close the handle!
    Code:
    .........
    {
       HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
       if(NULL != hProcess)
       {
          ::WaitForSingleObject(hProcess, INFINITE);
          CloseHandle(hProcess);
          AfxMessageBox(_T("Terminated"));
       }
    }

  14. #14
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994
    Originally posted by VictorN
    And don't forget to close the handle!.........
    Ok, thanks! I will remember.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  15. #15
    Join Date
    Jan 2004
    Posts
    56
    Originally posted by pengch
    Only the window is created by your current process.
    I think there's some way to subclass another process' window?. Anyway, we can't rely on a thread/process handle to verify if a window is closed. Process can have multiple threads, and thread can have multiple windows
    Trust urself!

Page 1 of 2 12 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center