📜 ⬆️ ⬇️

We write your Windows service

Many of us are faced with such a task when you need to run your application when you start your computer. Of course, you can put a shortcut in the startup, but somehow it is wrong. And besides, if the computer is overloaded and the user is not logged in, then your application will not start either.

The most correct decision in this situation is to write a Windows service.

Example of creating a service in Studio 2010, .Net C # under the cat

Step 1. Create a project.


')
Create a new project by selecting the Windows Service template .



Rename the class of service as you need.



Got this water code:

namespace ExampleSrv
{
public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}

protected override void OnStart( string [] args)
{
}

protected override void OnStop()
{
}
}
}


* This source code was highlighted with Source Code Highlighter .


This is, in fact, the service itself.
Use OnStart and OnStop events to accomplish your task.

Step 2. Add installer.


To make your service work, you need to install it.
To install it, it must have an installer.

Right-click ... Add installer



Now we have serviceProcessInstaller and serviceInstaller

img04

In the first, you can set the Account value in LocalSystem .
In the second, enter the name of the service, a description and do not forget to put StartType - Automatic .



The installer is ready.

Step 3. Logging.


In order for you to find out what your service was doing when it started, ended, or something else, you can use system logging.
This is done very easily.
Dragging from Toolbox to your EventLog service.



This is how logging is done:

public partial class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
}

protected override void OnStart( string [] args)
{
AddLog( "start" );
}

protected override void OnStop()
{
AddLog( "stop" );
}

public void AddLog( string log)
{
try
{
if (!EventLog.SourceExists( "MyExampleService" ))
{
EventLog.CreateEventSource( "MyExampleService" , "MyExampleService" );
}
eventLog1.Source = "MyExampleService" ;
eventLog1.WriteEntry(log);
}
catch {}
}
}


* This source code was highlighted with Source Code Highlighter .


Step 4. Installation.


To install the service, you need to call the installation utility and pass the parameter to the path to your service.
For this, I created an install.bat of this type:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe D:\...\ExampleSrv\bin\Debug\ExampleSrv.exe
pause


If you selected in the serviceProcessInstaller the value of the Account - User field, then during installation you will have to enter the login and password of the account under which the service will run. Attention! Write a domain in front of the user name is required!

We start the batch file with administrative rights and observe at the end:

The Commit phase completed successfully.
The transacted install has completed.

This means that the service is installed.
Checking:



Established.


A couple of times we do start and stop. Look logs:



We see when the service started and stopped.


Our service is ready.


The material is partially taken from here:
msdn.microsoft.com/en-us/library/zt39148a (VS.80) .aspx

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


All Articles