I am creating a windows service app which will run in background and check xyz.exe is running or not , if app is closed then after some interval of time it will open the app automatically
I am using a timer inside the service app but some how my app is opening in background instead of foreground, can some one help what's gone wrong here
Here is my code in windows service.exe :
protected override void OnStart(string[] args)
{
WriteToFile("Service is started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 15000; //number in milisecinds
timer.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
Process[] processes = Process.GetProcessesByName("xyz");
if (processes.Length == 0)
{
WriteToFile("xyz.exe is closed at " + DateTime.Now);
try
{
var p = Process.Start(@"C:\MyAPP\xyz.exe");
p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
}
catch(Exception exc)
{
WriteToFile("exception at " + exc);
}
}
else
{
WriteToFile("xyz.exe is Running at " + DateTime.Now);
}
}