Your code is not a restarter.
Code:
const int[] RestartHours = new int[]
{
00,
12
// ...
};
private static bool restarted;
private static void Restart()
{
restarted = false;
System.Threading.Thread restartThread = new System.Threading.Thread(
new System.Threading.ThreadStart(delegate
{
while (!restarted)
{
if (RestartHours.Contains(DateTime.Now.Hour) && !restarted)
{
System.Windows.Forms.Application.Restart();
}
System.Threading.Thread.Sleep(10000);
}
}));
restartThread.Start();
}
Add System.Windows.Forms to your references.
If you don't want to use that then use this method.
Code:
private const int[] RestartHours = new int[]
{
00,
12
// ...
};
private const string processName = "ConquerServer.exe";
private static bool restarted;
private static void Restart()
{
restarted = false;
System.Threading.Thread restartThread = new System.Threading.Thread(
new System.Threading.ThreadStart(delegate
{
while (!restarted)
{
if (RestartHours.Contains(DateTime.Now.Hour) && !restarted)
{
System.Diagnostics.Process.Start(processName);
Environment.Exit(0);
}
System.Threading.Thread.Sleep(10000);
}
}));
restartThread.Start();
}
Manual restart method:
Code:
private const string processName = "ConquerServer.exe";
private static void Restart()
{
System.Diagnostics.Process.Start(processName);
Environment.Exit(0);
}
Any problems with it let me know as I just did this out of my head.
Call Restart() a single time from the Main method.
Port check:
Code:
public static bool CheckLocalPort(int Port)
{
try {
System.Net.Sockets.Socket testSocket = new System.Net.Sockets.Socket();
testSocket.Bind(new System.Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), Port));
testSocket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
testSocket.Close();
return true;
}
catch {
return false;
}
}
public static bool CheckExternPort(int Port)
{
try {
System.Net.Sockets.Socket testSocket = new System.Net.Sockets.Socket();
testSocket.Connect(new System.Net.IPEndPoint(IPAddress.Parse("127.0.0.1"), Port));
testSocket.Disconnect(false);
return true;
}
catch {
return false;
}
}
Example usage:
Code:
Console.WriteLine(CheckLocalPort(80));//checking port 80 and writing true if available and false if not.