Search code examples
c#windowsservicewindows-services

C# my application starts in task manager but not shown in Desktop


I created a windows service with Top Shelf. This service tests the battery level and when battery successfully charged(80%) it opens any UI Windows Form or application to nodify me about that (In this example it opens .txt). When i test it in Visual Studio in works perfectly so my UI or any application apperas both in Task Manager and Desktop but when i install it as service, UI or any application appears only in Task Manger not in Desktop here is code:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Timers;
using System.Windows.Forms;

namespace SimpleHeartBeatService
{
    public class Heartbeat
    {
    private readonly System.Timers.Timer _timer; //referance yaratdiq
    private static bool firstChecker = true;
    private static bool secondChecker = true;
    public Heartbeat()
      {
      _timer = new System.Timers.Timer(1000) //referanca objecr menimsetdik 
        {
            AutoReset = true  //Object initializer (kitabda Constr deeper de en axirda var)
        };

        _timer.Elapsed += TimerElapsed;
    }

    private void TimerElapsed(object sender, ElapsedEventArgs e)
    {  //Bize lazim olan hisse burdan asagidadir

        PowerStatus status = SystemInformation.PowerStatus;
        if (status.PowerLineStatus.ToString() == "Online" && secondChecker)
        {
            firstChecker = true;
            secondChecker = false;
        }

        if (firstChecker && status.PowerLineStatus.ToString() == "Online" && 
                                                               status.BatteryLifePercent * 100 > 80)
        {
            Process.Start(@"C:\Users\qarib\Desktop\Test\batteryLevel.txt");


            firstChecker = false;
        }

        if (status.PowerLineStatus.ToString() == "Offline")
        {
            secondChecker = true;
        }
        // code here
        Thread.Sleep(500);
    }

    public void Start() //Bunlar mutleq yazilmalidir
    {
        _timer.Start();
    }

    public void Stop() //Bunlar mutleq yazilmalidir
    {
        _timer.Stop();
    }
}

}


Solution

  • http://securityinternals.blogspot.com/2014/02/windows-session-0-isolation.html

    You can read the details in the article, but suffice it to say that Windows services cannot interact with the desktop of regular users anymore. My guess is that your form is actually opening in Session 0, which is not something you'll normally be able to view or interact with.

    In order to get around this problem, here's what my project has done.

    We have created a Windows service that exposes some mechanism for communicating with it. In our case, the Windows service originally exposed a WCF-based interface using both sockets and pipes. Because of the overhead, we eventually moved to a TCP socket over localhost, and today we are using shared memory. The mechanism doesn't really matter for the purpose of this discussion. Your service just has to provide some way for a user-run application to connect, communicate, and disconnect.

    Once this is in place, you can build an application that provides a UI for communicating with the service. We've done this and allowed the application to be minimized to the system tray to give the appearance that it's a behind-the-scenes thing. The system tray icon then provides a popup menu that allows the user to send commands directly to the service or open the application for additional features.

    HTH