Results 1 to 10 of 10

Thread: [RESOLVED] Application restarting itself

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Resolved [RESOLVED] Application restarting itself

    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?

    Any ideas how this can be accomplished?

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: Application restarting itself

    Try this Bezzie.

    Main Project :

    Code:
    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!
    Attached Files Attached Files
    Last edited by HanneSThEGreaT; Jan 22nd, 2010 at 08:35 AM.
    VB.NET MVP 2008 - Present

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Re: Application restarting itself

    Hi Hannes

    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.

  4. #4
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: Application restarting itself

    Just out of curiosity.

    Can you not just start a new instance of your application form your current instance and then close you current instance ?

    As in, MainApp is running and needs to restart. thus your Restart method within your MainApp says:
    VB Code:
    1. Call Shell("MainApp.exe", 1)

    Then unload your forms and close your current instance while your other "restarted" instance is loading up.

    Would that not work somehow ?

    Edit
    It seems to work.
    I started a new VB6 project, added a CommandButton to the form with the following Code:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Call Shell(App.Path & "\" & App.EXEName & ".exe", 1)
    3.    
    4.     Unload Form1
    5.     Set Form1 = Nothing
    6. End Sub

    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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Re: Application restarting itself

    Optional:

    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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Re: Application restarting itself

    Hannes:

    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.

  7. #7
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: Application restarting itself

    Quote Originally Posted by Bezzie View Post
    Optional:

    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:
    1. 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
    2.  
    3. Private Sub Command1_Click()
    4.     Call ShellExecute(Me.hwnd, "Open", App.Path & "\" & App.EXEName & ".exe", "Restart", "", 1)
    5.     Unload Form1
    6.     Set Form1 = Nothing
    7.     'Call Shell(App.Path & "\" & App.EXEName & ".exe", 1)
    8. End Sub

    You could then write code in your application checking the command line like this:
    VB Code:
    1. If (Command = "Restart") Then
    2.     '-- Do not check for previous instance.
    3. Else
    4.     '-- check for previous instance.
    5. 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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Roodepoort, South Africa
    Posts
    472

    Re: Application restarting itself

    Hannes: Your example is working just fine.

    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.

    Thanks for your input guys.

  9. #9
    New Member
    Join Date
    Apr 2008
    Posts
    6

    Re: Application restarting itself

    This is just what I needed to force a re-start of my program in order to enforce logging in. Many thanks for the posts!

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Application restarting itself

    I hate to add to the gravedigging, but people should be aware of the hazards and failures of FindWindow.

    See Avoiding Multiple Instances of an Application which covers many of the issues nicely.

    In particular:

    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.

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