I Wrote a windows service, you can find code below
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new AutoUpgradeServiceDemo()
};
ServiceBase.Run(ServicesToRun);
}
//Service class
public AutoUpgradeServiceDemo()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
MessageBox.Show("Service Has been Started");
//My logic goes here
}
protected override void OnStop()
{
//My logic goes here
MessageBox.Show("Service Has been Stopped");
}
after installing, i started it by manually. It is immediately closing showing following error.
Could anyone help me...
Windows services have no user interface. So you cannot show a message box from a service. If you want to report errors, the standard way is using event log.
protected override void OnStart(string[] args)
{
base.OnStart(args);
//my stuff
this.WriteEventLogEntry("My service started successfully", System.Diagnostics.EventLogEntryType.Information);
}
Windows service must execute OnStart()
in limited time, or is killed. How many seconds? I don't remember, look in MSDN. When service is killed in this scenario, not too much info is reported, maybe Your is killed by watchdog too?
If longer start-up is required I usually start new thread
protected override void OnStart(string[] args)
{
MyXxxxxStarter.LogInfo("Try to start thread");
worker = new Thread(WorkerThread);
worker.Start();
MyXxxxxStarter.LogInfo("Thread started ");
}
I agree with TSungur, logging only by event log (in my code implemented in one place)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.