VB.net Auto Restart Application

I have a code here that will start a process and when pressed again it will kill the current process and start a new one(restart).

   

     Private Games As New Dictionary(Of String, Process)
   
    Private Sub GS1_Click(sender As Object, e As EventArgs) Handles GS1.Click
        Dim gameservercfg As String = GameServer1.Text
        Dim Key As String = gameservercfg.ToUpper
   
        If Games.ContainsKey(Key) Then
            If Not Games(Key).HasExited Then
                Games(Key).Kill()
            End If
            Games.Remove(Key)
        End If
   
        Dim psi As New ProcessStartInfo
        psi.FileName = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "GameServer.exe")
        psi.WorkingDirectory = System.IO.Path.GetDirectoryName(psi.FileName)
        psi.Arguments = gameservercfg
        Games.Add(Key, System.Diagnostics.Process.Start(psi))
    End Sub


What I want to do is, how to make it auto restart when that gameserver.exe crashes by itself?
Thank you!
LVL 1
Kerr Heilee AlmarioAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Here's another example...

Set Process.EnableRaisingEvents to True, and wire up the Process.Exited event where you can restart it again:
    Private Sub GS1_Click(sender As Object, e As EventArgs) Handles GS1.Click
        Dim psi As New ProcessStartInfo
        psi.FileName = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "GameServer.exe")
        psi.WorkingDirectory = System.IO.Path.GetDirectoryName(psi.FileName)
        psi.Arguments = GameServer1.Text
        Dim P As Process = System.Diagnostics.Process.Start(psi)
        P.EnableRaisingEvents = True
        AddHandler P.Exited, AddressOf P_Exited
    End Sub

    Private Sub P_Exited(sender As Object, e As EventArgs)
        Dim P As Process = DirectCast(sender, Process)
        P.Start() ' restart it
    End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kerr Heilee AlmarioAuthor Commented:
Hi Mike, won't this solution will restart everything even you close the .exe in purpose?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
That's true.  The Exited() event will fire when that process has exited, regardless of how it occurred.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Kerr Heilee AlmarioAuthor Commented:
Is there a way to have a button that will kill the instance and prevent it from starting?
Or just close the app and close the .exe which is an easy temporary fix for the problem?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
If you want to Kill() it without restarting it, then either remove the handler, or setup a boolean variable that you toggle before killing it, then check the variable and only restart when appropriate.
Kerr Heilee AlmarioAuthor Commented:
Thanks Mike!
Kerr Heilee AlmarioAuthor Commented:
Hey there Mike, just another question. Right now, It restarts automatically when closing the .exe and when clicking the button, it will start the .exe, and when you click again, it will open the same .exe which it shouldn't be. Something like when you hit the button at first run, it will convert the button to kill the process so the program will auto restart it.
How can I do that when you click the button again, it will restart as well? I hope you understand what I mean. Just ask me here if you need more explanation.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I think this is what you're asking for...repeat the below code for each Button/TextBox pair:
Public Class frmProcesses

    Private WithEvents P1 As Process

    Private Sub frmProcesses_Load(sender As Object, e As EventArgs) Handles Me.Load
        TextBox1.Text = "C:\Users\mikes\Documents\SomeFile.txt"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not IsNothing(P1) Then
            Dim tmpProc As Process = P1
            P1 = Nothing
            tmpProc.CloseMainWindow()
            Exit Sub
        End If

        Dim psi As New ProcessStartInfo
        psi.FileName = System.IO.Path.Combine("C:\Windows\System32", "Notepad.exe")
        psi.WorkingDirectory = System.IO.Path.GetDirectoryName(psi.FileName)
        psi.Arguments = TextBox1.Text

        P1 = System.Diagnostics.Process.Start(psi)
        P1.EnableRaisingEvents = True
    End Sub

    Private Sub P1_Exited(sender As Object, e As EventArgs) Handles P1.Exited
        P1.Start() ' restart it
    End Sub

End Class

Open in new window

Kerr Heilee AlmarioAuthor Commented:
I really appreciate it Mike! It works flawless. I can't put it as another solution?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I wouldn't worry about it.  Anyone reading the thread will be able to see the different solutions.  Thanks, though.
Kerr Heilee AlmarioAuthor Commented:
Hey there, is there any way I can put a checkbox to make the auto restart optional? I know i'll just make the raisingevent turn to false but whenever I try to start it with checkbox checked then i unchecked it while the process is running it's still restarting itself, but when I start it with the unchecked checkbox its working fine.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
then i unchecked it while the process is running it's still restarting itself

You'd need to use the CheckBox in the exited event as well:
    Private Sub P1_Exited(sender As Object, e As EventArgs) Handles P1.Exited
        If cbRestart1.Checked Then
            P1.Start() ' restart it
        End If
    End Sub

Open in new window

Kerr Heilee AlmarioAuthor Commented:
Fixed. Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.