1

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?

Foxseiz
  • 290
  • 1
  • 6
  • 20

1 Answers1

1

The basic idea that could work in such a scenario is that you save any configuration to a file onto the disk. Then restart the application using

System.Windows.Forms.Application.Restart()

And on application start event load any data from the disk file where you saved your configuration, if any. For more insight into the topic you may also read these SO posts
auto-restart-and-then-continue-the-sub-in-vb-net
application-restart-puzzling-behaviour-in-vb-net

Additionally you may (1) save your configuration, (2) start a new independent instance of the same application and (3) then end the current instance.
Do not forget to load saved/default configurations from the saved file on application start/restart, if required.

Community
  • 1
  • 1
haraman
  • 2,744
  • 2
  • 27
  • 50