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

Thread: WaitForSingleObject Question

  1. #16
    Join Date
    Oct 2003
    Location
    Romania
    Posts
    127
    Originally posted by sephiroth2m
    U can also subclass that window then intercept its WM_DESTROY message.
    How to do this? Post a short example.

  2. #17
    Join Date
    Jan 2004
    Posts
    56
    There's several ways to do this, here is one (DLL) that works fine on Win9x/NT/2K/XP. The SetMyHook(HWND my_window, HWND subclassed_window) function subclasses the 'subclassed_window' in another process and PostMessage to 'my_window' when it's destroyed. ReleaseMyHook() unsubclasses it.
    Regards
    Attached Files Attached Files
    Trust urself!

  3. #18
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175
    Exactly why I wait for the Window to close instead of the process. I have noticed that when this window is closed the process is still running. This is why I watch for the window to close, then use terminate process on the running process.

    I would change the code of the application, but it is a vendor application that they don't allow changes to.

  4. #19
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Hm.. I would use TerminateProcess only on extreme cases. It is kind of an ugly brute force technique.

    Well, how would the other process terminate otherwise? I mean, the normal case.

  5. #20
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175
    It doesn't until you reboot the machine. It is a Java Applet using the MVM to run. It opens a port that I need to be closed and reopened every time, but this port is locked as long as the process it running.

  6. #21
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Hm.. interesting. Does that applet provide some protocol for closing the port at all ?

  7. #22
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175
    Doesn't look like it, but I am not allowed to view the code since it is a vendor application.

  8. #23
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    You may not have a source code, but some vendors implement some sort of interprocess communcation to allow for such things.

    I remember one where we had to send UDP packets to a particular port and another one where we had to change an xml file to update certain things.

  9. #24
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175
    I implemented VictorN's 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"));
    }
    }

    But it didn't work since the process was still running after I closed the window. Thanks for your help - any other ideas?

  10. #25
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524
    1. I need to correct you: ovidiucucu had suggested this code, not Victor !
    2. What does it mean "it didn't work "?
    a) dwThreadID == 0 ?
    b) hProcess == NULL ?
    c) WaitForSingleObject doesn't return ?

    In case of c) - you will need to ::TerminateProcess(hProcess );

    regards,
    Victor

  11. #26
    Join Date
    Oct 2003
    Location
    Minnesota
    Posts
    175
    Sorry about that, my mistake.

    What doesn't work is the WaitForSingleObject because it is waiting on a process that doesn't quit. I need a way to find out when the window is closed so that the process can be terminated through TerminateProcess.

  12. #27
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,058
    Code:
    DWORD dwProcessID = 0;
    DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
    if(0 != dwThreadID)
    {
       HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
       if(NULL != hProcess)
       {
          while(::IsWindow(hWnd) )
          {
             ::Sleep(200);
          }
          ::TerminateProcess(hProcess, 0);
          ::CloseHandle(hProcess);
       }
    }
    To Victor,
    As promissed, I have not forgot CloseHandle
    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/

  13. #28
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,524
    2 ovidiucucu :


    Victor

  14. #29
    Join Date
    Jun 2004
    Posts
    17
    have you read .....

    CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ FAQs > Threads: How to end a thread?

    http://www.codeguru.com/forum/showth...hreadid=231242

    and what Jeffrey Richter has to say ...

    http://www.microsoft.com/msj/0799/Win32/Win320799.aspx
    Last edited by jono; July 5th, 2004 at 01:51 AM.

Page 2 of 2 FirstFirst 12

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center