0

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;
            } 
        }
    }
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
Khushi
  • 49
  • 1
  • 3
  • 7
  • open local services manager and search for your service and make sure it is started. – YOusaFZai Dec 27 '13 at 05:39
  • the service is started and it executes the line of code which shuts down the machine but it doesnot show the pop up. Like the while loop is getting executed but the pop up is not coming – Khushi Dec 27 '13 at 05:43
  • from where you are calling that pop up? – YOusaFZai Dec 27 '13 at 05:46
  • i am calling the pop up from service. the exe which m calling is WPF app which is pop up – Khushi Dec 27 '13 at 07:37

1 Answers1

0

Do not try to display UI from windows service (even via launching of separate process).
It was never easy, and since Windows 8 interactive services are disabled by default (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\NoInteractiveServices == 1).

Use separate GUI application, which runs on user logon and listens for service messages.

Dennis
  • 37,026
  • 10
  • 82
  • 150