'Any ideas why the server I am launching would be crashing when a request is made to it?
The server works perfectly when I run it directly. When I launch the server using the following code, it is closing and then restarting.. any ideas what I might be missing? My initial thought was the server was triggering the OnChange event when a request was being made to it. But no changes are logged.. I'm stumped.
internal static class Program
{
private static Timer _timer = new Timer
{ Interval = 1000, Enabled = true, AutoReset = true };
private static DateTime _lastChange;
private static void Main()
{
using var watcher =
new System.IO.FileSystemWatcher(
ConfigurationManager.AppSettings["RootWatchDirectory"])
{
NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.Security
| NotifyFilters.Size
};
watcher.Changed += OnChanged;
_timer.Elapsed += OnInterval;
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
Console.ReadLine();
}
private static void OnInterval(object sender, ElapsedEventArgs e)
{
var now = DateTime.Now;
var processes =
Process.GetProcessesByName(server);
var changeDetectWaitTimeInSeconds = int.Parse(10);
if (processes.Length != 0) return;
var ticksElapsed = now.Ticks - _lastChange.Ticks;
var ticksToWait = TimeSpan.FromSeconds(changeDetectWaitTimeInSeconds).Ticks;
if (ticksElapsed > ticksToWait)
PowerShell
.Create()
.AddCommand("Start-Process")
.AddParameter("FilePath",
"server.exe")
.Invoke();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed) return;
_lastChange = DateTime.Now;
Console.WriteLine($"Changed: {e.FullPath}");
PowerShell
.Create()
.AddCommand("Stop-Process")
.AddParameter("Name", "server")
.Invoke();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
