How do you have your application restart itself. My application checks for updates on startup and some of the updates require a restart of the application.
I've tried making a second app called Restart which can be called from my app. The Restart app is supposed to wait a while so the original app has time to finish closing but it doesn't work.
From my app I call Restart with the Shell function and immediately after the call to Shell, End (also tried Unload Me) like:
Code:
Call Shell(App.Path & "\Restart.exe", vbNormalFocus)
End
The Restart app starts up no problem but...
The original app doesn't close It seems that it is waiting for the call to Shell to finish before it will close.
So after the Restart app waited a couple of seconds (I had it wait for up to 10 seconds), all the while the original app just sits there, it kicks off the new instance of the original app which tries to start but then gets caught by the App.PrevInstance check in the original app.
Why doesn't my original app close when the End or Unload Me call are made?
Private Sub Command2_Click()
Call Shell(App.Path & "\Restart.exe", 1)
End Sub
Restart project :
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Private Sub Form_Load()
Dim WinWnd As Long
WinWnd = FindWindow(vbNullString, "Form1") 'Replace Form1 with Whatever your Form's Caption is
If WinWnd <> 0 Then
PostMessage WinWnd, WM_CLOSE, 0&, 0&
Else
MsgBox "No window of that name exists."
End If
Call Shell("Main.exe", 1)
End Sub
I hope it helps
EDIT: Initially added wrong zip Fixed now!
Last edited by HanneSThEGreaT; Jan 22nd, 2010 at 08:35 AM.
Going to try your example. Will have to pass the Form.Caption as a parameter to the Restart App as the Caption would change depending on the updates and registration details of the original app.
Compiled the application to an Executable and run the executable.
Worked fine, was able to re-start consecutively over and over.
Off course, in your case you might need to unload and clear more than 1 form but it's just an example on restarting yourself without an additional application.
If you feel you need another application to manage the restart, then why not write a service to act as "Master" Application (Or similar).
This way your main application is not the one in charge and your service can be told what application to start or re-start.
Though if you only have 1 application in general that might be overkill.
Last edited by Optional; Jan 22nd, 2010 at 09:47 AM.
Kind Regards,
Optional
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
The program checks if there is any previous instance of the program running. It doesn't allow a second instance to run. One has to close before another can run.
At this stage I can't get the first one to close before the second one wants to run and then the App.PrevInstance catches the second one and stops it.
Still having the same problem. The first instance of the program just sits there on the screen until the Restart.exe finishes. Restart.exe finishes right after calling the Shell to the main program but the App.PrevInstance still catches the second instance before the first one finishes.
The program checks if there is any previous instance of the program running. It doesn't allow a second instance to run. One has to close before another can run.
At this stage I can't get the first one to close before the second one wants to run and then the App.PrevInstance catches the second one and stops it.
Have you considered using a command line parameter ?
When restarting yourself use ShellExecute instead of Shell.
ShellExecute will let you pass a command parameter to the application your are starting, something like this:
VB Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
You could then write code in your application checking the command line like this:
VB Code:
If (Command = "Restart") Then
'-- Do not check for previous instance.
Else
'-- check for previous instance.
End If
I'm just throwing out ideas.
Last edited by Optional; Jan 22nd, 2010 at 04:45 PM.
Reason: fixed spelling
Kind Regards,
Optional
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
It was my bad. I used the Sleep function to make the Restart app wait a couple of seconds and this is what made the main app not close. Changed it to a loop, with a DoEvents in the loop, that runs for a couple of seconds and voila, the main app closes immediately as it should and the Restart app restarts the main app as it should.
Daniel Lohmann, who has made significant contributions to this article, also points out that in terms of "uniqueness", FindWindow has a problem in that it enumerates only windows in the same desktop as the calling thread. Therefore, if there is another instance running on another desktop you won't find it to pop it up!
If you don't know what a "desktop" is and why it matters you have no business trying to write auto-updaters. Seek a commercial solution.