I have created a Windows service which kicks off a WPF pop-up at specific time. The service works fine in debug mode, but when I install the service, it does not show the pop-up but it does execute the commands after the pop-up command. I tried allowing the service to interact with desktop, but that also is not working. In this code the line which shuts down the machine gets executed after I install the service, but it does not kick off the executable (which is the WPF pop-up window).
Process p = new Process();
System.Timers.Timer _timer;
int intervalMins = 10;
bool IsInvokedToday = false;
DateTime startAt = DateTime.Now.AddHours(16);
public Shutdownservice()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
_timer = new Timer(intervalMins * 1000);
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start();
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (DateTime.Now > DateTime.Today.Add(new TimeSpan(16, 0, 0)))
{
countForNoResponse();
}
}
protected override void OnStop()
{
}
public void countForNoResponse()
{
Process p = new Process();
int count = 0;
while (count < 4)
{
p.StartInfo = new ProcessStartInfo(@"C:\Windows\Temp\ShutdownSystem.exe");
p.Start();
bool wait = p.WaitForExit(1000 * 5);
if (!wait)
{
p.Kill();
System.Threading.Thread.Sleep(1000 * 5);
count++;
if (count == 4)
{
Process.Start("shutdown", "/s /t 900");
}
}
else
{
count = 6;
}
}
}