Results 1 to 10 of 10

Thread: Unable to bring form to front after calling WaitForSingleObject()

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    9

    Unable to bring form to front after calling WaitForSingleObject()

    In my vb6 program I need to use WaitForSingleObject on a separate shell launched program, kind a emulating a modal-style behavior. Now, while my vb6 app is waiting for this second program is does not re-paint, prevents the user from seeing their desktop if they wish to view it, and makes it seem as though the program has stopped responding (Which, I mean, technically it has). So I minimize the program for the brief period they're using the second program, the work flow works fine and I'm happy with that.

    My problem appears once this second program has completed and I try to bring my vb6 program back up and to the front. It seems that I am unable to do so through simply maximizing the window state, BringWindowToTop(), ShowWindow(), SetWindowPos(), SetActivewindow(), SetForegroundWindow() or any combination of these. The only way I've been able to make my program come to the front is by setting it to Always On Top. I've tried looping the code until GetForegroundWindow and GetActiveWindow are the windows I expect them to be but unfortunately that doesn't seem to work. The code reports back that my program is the active window and also the foreground window but I can see that is not the case. Whatever window became active when I minimized my program is what remains in front.

    My program has a MDIParent/child thing going on so I tried alternating the two of those to be the desired front form but changing that had no effect. I'm out of ideas and I really don't know why this won't work for me.

    I think it's worth noting that although this problem happens the first time, subsequent launches of the secondary program have it returning just fine with no issue, which is especially frustrating. Code below.

    Code:
    hProcessID = Shell("""SecondProgram.exe"" print" + fileLoc, vbNormalFocus)
        
        
        If hProcessID <> 0 Then
            Call frmMDIChild.WaitForAppTerminate(hProcessID)
        End If
    
    Public Sub WaitForAppTerminate(hProcID As Long)
       Dim hProcess As Long
       hProcess = OpenProcess(SYNCHRONIZE, 0, hProcID)
    
       If hProcess <> 0 Then
          frmMDIParent.WindowState = vbMinimized
          Call WaitForSingleObject(hProcess, INFINITE)
          BringWindowToTop frmMDIParent.hwnd
          ShowWindow frmMDIParent.hwnd, 3
          SetActivewindow frmMDIParent.hwnd
          SetForegroundWindow frmMDIParent.hwnd
          DoEvents
       End If
    Any help or insight would be greatly appreciated!

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,288

    Re: Unable to bring form to front after calling WaitForSingleObject()

    I assume that all of those are API calls?

    Have you tried simply using normal VB code

    i.e. frmMdiParent.show

    or
    frmMDIParent.WindowState = vbNormal
    frmmdiparent.setfocus

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,685

    Re: Unable to bring form to front after calling WaitForSingleObject()

    You should close that process handle after your WaitForSingleObject call. I don't expect it to fix your problem but that code will be leaking handles which has the potential to cause other problems.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    9

    Re: Unable to bring form to front after calling WaitForSingleObject()

    I'm afraid I did try those DataMiser, but saw even less functionality, with my code above I usually at least see the program come out of the tray but sitting behind the active window (If it permits it like having an explorer window opened), with .Show, or just setting the WindowState to normal it never leaves the tray and I don't see it at all.

    Thanks Niya, I added that in but as you said, it didn't change my problem.

  5. #5
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Unable to bring form to front after calling WaitForSingleObject()

    You may use WSCript.shell to run your "secondprogram.exe" and using its inner properties to "wait" for it to close prior to continuing with the code.

    Code:
    Private Sub Command_Click()
    
        'You can use any code you want here that will be executed BEFORE secondprogram.exe will run
         Me.Enabled = False
    
         Dim oShell As Object
         Set oShell = CreateObject("WSCript.shell")
         oShell.run "cmd /C " & "secondprogram.exe", 0, True
    
        'You can use any code you want here that will be executed AFTER secondprogram.exe goes down
         Set oShell = Nothing
         Me.Enabled = True
    
    End Sub
    Let me know if this helps
    Last edited by stum; Apr 16th, 2015 at 01:30 PM. Reason: simplifying

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,482

    Re: Unable to bring form to front after calling WaitForSingleObject()

    Forcing yourself to the foreground will not always work and may depend on which window is currently in the foreground and whether or not it has the keyboard focus. Here is a link (not in VB) that addresses a couple of workarounds. If you are familiar enough with APIs, the 2nd approach would be easy to convert to VB. If nothing else is working, give this a shot?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    9

    Re: Unable to bring form to front after calling WaitForSingleObject()

    Hi Stum, that code works fine, but allows the user to continue working in the vb6 app while the second program is open which I need to prevent.

    I'll look over that information LaVolpe, it looks like it may come to that.

  8. #8
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Unable to bring form to front after calling WaitForSingleObject()

    Disabling the user from acting within your application should not be to hard to do.

    Code:
    Private Sub Command_Click()
    
        'You can use any code you want here that will be executed BEFORE secondprogram.exe will run
         Call DisableAllForms(True)
    
         Dim oShell As Object
         Set oShell = CreateObject("WSCript.shell")
         oShell.run "cmd /C " & "secondprogram.exe", 0, True
    
        'You can use any code you want here that will be executed AFTER secondprogram.exe goes down
         Set oShell = Nothing
         Call DisableAllForms(False)
    
    End Sub
    Code:
    Public sub DisableAllForms(boolToDisable as Boolean)
        Dim frm As Form
     
        For Each frm In Forms
            If (toDisableForms = True) Then
                frm.Enabled = False
            Else
                frm.Enabled = False
            End If
        Next frm
    End Sub
    Or something like that.
    You can also Hide all forms or, check which forms are visible, and hide only them, then retrieve visibility when app is done, you may even want to draw a big frame ontop of everything that will block the user from touching stuff, options are endless.
    Last edited by stum; Apr 16th, 2015 at 02:13 PM.

  9. #9
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Unable to bring form to front after calling WaitForSingleObject()

    Quote Originally Posted by BryanMcN View Post
    My problem appears once this second program has completed and I try to bring my vb6 program back up and to the front. It seems that I am unable to do so through simply maximizing the window state, BringWindowToTop(), ShowWindow(), SetWindowPos(), SetActivewindow(), SetForegroundWindow() or any combination of these.
    You may want to try a few more APIs.

    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,972

    Re: Unable to bring form to front after calling WaitForSingleObject()

    did you try appactivate?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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