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();
}
}
}