Kerr Heilee Almario
asked on
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(Dir ectory.Get CurrentDir ectory(), "GameServer.exe")
psi.WorkingDirectory = System.IO.Path.GetDirector yName(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!
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(Dir
psi.WorkingDirectory = System.IO.Path.GetDirector
psi.Arguments = gameservercfg
Games.Add(Key, System.Diagnostics.Process
End Sub
What I want to do is, how to make it auto restart when that gameserver.exe crashes by itself?
Thank you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's true. The Exited() event will fire when that process has exited, regardless of how it occurred.
ASKER
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?
Or just close the app and close the .exe which is an easy temporary fix for the problem?
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.
ASKER
Thanks Mike!
ASKER
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.
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.
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
ASKER
I really appreciate it Mike! It works flawless. I can't put it as another solution?
I wouldn't worry about it. Anyone reading the thread will be able to see the different solutions. Thanks, though.
ASKER
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.
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
ASKER
Fixed. Thank you.
ASKER