|
-
Apr 16th, 2015, 12:42 PM
#1
Thread Starter
New Member
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!
-
Apr 16th, 2015, 12:58 PM
#2
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
-
Apr 16th, 2015, 01:04 PM
#3
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.
-
Apr 16th, 2015, 01:13 PM
#4
Thread Starter
New Member
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.
-
Apr 16th, 2015, 01:22 PM
#5
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
-
Apr 16th, 2015, 01:22 PM
#6
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?
-
Apr 16th, 2015, 01:40 PM
#7
Thread Starter
New Member
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.
-
Apr 16th, 2015, 02:09 PM
#8
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.
-
Apr 17th, 2015, 09:04 AM
#9
Re: Unable to bring form to front after calling WaitForSingleObject()
 Originally Posted by BryanMcN
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)
-
Apr 17th, 2015, 04:24 PM
#10
Re: Unable to bring form to front after calling WaitForSingleObject()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|