How is code executed in a window service?

I was asked to create a Windows C # service. However, I use to create a GUI with user input.

Since Windows services are automated, I would like to know how the code is executed.

I mean, how can I control the flow?

Can anyone clarify? I do not find much information about window services ...

+4
source share
3 answers

The Windows service starts exiting OnStart, usually re-execution starts from this moment, for example, there may be a timer. When the service stops the OnStop method. This article could be a good starting point.

protected override void OnStart(string[] args) { base.OnStart(args); //TODO: place your start code here } protected override void OnStop() { base.OnStop(); //TODO: clean up any variables and stop any threads } 
+2
source

Code runs in OnStart()

 protected override void OnStart(string[] args) { // Equivalent of Main() // Run threads here before timeout so OS knows it has started } 

Typically, you start a thread from another function so that OnStart() can return and start the service.

Same thing with OnStop and OnShutdown , etc., where you clear everything.

+2
source
 protected override void OnStart(string[] args) { try { timer.AutoReset = true; timer.Enabled = true; timer.Start(); serviceThread = new Thread(new ThreadStart(Delete)); clientCleanupThread = new Thread(new ThreadStart(removeExpirery)); enableAutoSubscribeProduct = new Thread(new ThreadStart(Products)); serviceThread.Start(); clientCleanupThread.Start(); enableAutoSubscribeProduct.Start(); } catch (Exception ex) { Log.Error("Error on thread start " + ex.Message); } } 
0
source

Source: https://habr.com/ru/post/1444608/


All Articles