If i send a WM_CLOSE message my program freezes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IronRazer
    New Member
    • Jan 2013
    • 83

    If i send a WM_CLOSE message my program freezes

    I am working on a program that sends keys, button clicks, and so on to external windows. If i send a close command to notepad or wordpad and it pops up the (save file dialog window) it freezes my program. I can`t even click on my program window to bring it back into focus until i click yes, no, or cancel on the (save file dialog window). This stops my program from finding the dialog window so i can send it a button click. Why is it freezing my program ?

    This is an example of my code:
    Code:
        Private Sub TSB_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSB_Close.Click
            SendMessage(hWnd, WM_CLOSE, 0, 0)
    
            'It seems to freeze here if a popup Dlg window is created by the
            'program that is recieving the close message. If no popup Dlg
            'window is created then it continues with no problems.
    
            CheckForPopupDlgWindow(LB_Id.SelectedItem.ToString)
        End Sub
    
        Private Sub CheckForPopupDlgWindow(ByVal parentID As String)
            'This is the Class of the popup dialog window to find
            Dim popupDlgclass As String = "#32770"
    
            'Get desktop handle
            Dim hWnddesktop As IntPtr = IntPtr.Zero
            hWnddesktop = GetDesktopWindow()
    
            'Find the first desktop child window with the same class as (popupclass)
            Dim popuphandle As IntPtr = IntPtr.Zero
            popuphandle = FindWindowEx(hWnddesktop, popuphandle, popupDlgclass, IntPtr.Zero)
    
            'if a (popuphandle) is found with the same class then check if it has same ID as (parentID)
            While (Not popuphandle.Equals(IntPtr.Zero))
    
                'Get the ID for (popuphandle)
                Dim foundProcID As UInt32 = Nothing
                GetWindowThreadProcessId(popuphandle, foundProcID)
    
                'If the (popuphandle) has the same ID as the parentID then add it to listbox and stop looking
                If foundProcID.ToString = parentId Then
                    LB_DlgPopupWindows.Items.Add(popuphandle.ToString)
                    Exit While
                End If
    
                'Get the next desktop child window to check if one has not been found
                popuphandle = FindWindowEx(hWnddesktop, popuphandle, popupDlgclass, IntPtr.Zero)
            End While
        End Sub
    Last edited by IronRazer; Jan 31 '13, 08:47 PM. Reason: This is my question. Tried to add color so it could be read easy.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    To be picky it doesn't freeze your program - it freezes that thread.
    If your program is single threaded then you are fraked.

    So you probably need to make it multi-threaded so it can continue to look for these new pop-ups and handle them.

    Comment

    Working...