1

I'm currently developing a little game server and it's having a "Emulator". Sometimes it's crashing and the users will have to wait till I'm back from work, so I wanted to ask if someone knows, if there is a way to auto restart the .exe (there are 3 of them) if it crashes, so if the program says "... stopped working".

Is there anybody to help me out?

Venace
  • 13
  • 2
  • 3
    A "little game **server**" should probably be a Windows Service, which has built-in auto-restart on crash. – Fildor Sep 15 '17 at 11:48
  • 1
    ... outside that you probably should fix the crashes ;) – Fildor Sep 15 '17 at 11:52
  • _the program says "... stopped working"_ If the program does not **completely** exit you may have to go the route of enumerating windows periodically to look for the existence of that error message and then kill and restart the app. Look for examples using `EnumWindows()` like in [this thread](https://stackoverflow.com/questions/19867402/how-can-i-use-enumwindows-to-find-windows-with-a-specific-caption-title). You might also use `FindWindow()` to search for that error message by **class type**. – Idle_Mind Sep 15 '17 at 12:33

4 Answers4

0

You can write a simple batch file which will check the return value of the exe's main function (exit-code).

Or just write a simple program to start it with Process.Start if it's not running -> enumerate and check Process.GetProcesses().

You can also create a service, if the 'server' does not provide a programmed one itself.

There are many options...

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
0

Standing on a sinking ship, it is hard to rescue yourself from the sinking ship... Your best bet is a second process, some kind of watchdog checking whether the application is still alive and automatically starts it if not.

You can enumerate running processes by calling

System.Diagnostics.Process.GetProcesses
mbnx
  • 912
  • 5
  • 11
0

Subscribe to Process.Exited event to restart it when it exits:

var proc = Process.GetProcessesByName("my-server").Single();
proc.EnableRaisingEvents = true;

proc.Exited += (a, e) =>
{
    proc = Process.Start(...);  // Restart the process
};
Pavel Tupitsyn
  • 8,393
  • 3
  • 22
  • 44
0

There are various ways you could do this Keeping in mind you have your check condition beforehand check For example

If(stopped working){
 ProcessStartInfo Info = new ProcessStartInfo();
Info.Arguments = "/C ping 127.0.0.1 -n 2 && \"" + Application.ExecutablePath 
+ "\"";
  Info.WindowStyle = ProcessWindowStyle.Hidden;
Info.CreateNoWindow = true;
Info.FileName = "cmd.exe";
Process.Start(Info);
Application.Exit();
}

Another way would be

If(stopped working){
Application.Restart();
 }

Another way would be

If(stopped working){
Process.Start(Application.ExecutablePath); 
}
AnkUser
  • 5,421
  • 2
  • 9
  • 25