Microsoft Windows services (formerly called NT services) allow users to create executable applications that can run for a long time in their own windows sessions. These services can be started automatically when the computer starts, can be paused and restarted, and do not display any user interface. These features make the service ideal for use on the server or for features that need to run for a long time without affecting other users working on the same computer. It feels that the function is similar to that of Windows task plan, but the task plan can flexibly set the time for running executable applications. It can be a program with user interface. Window service is more like an invisible hero, running silently in the background.
The demonstration environment: VS2019, windows 10 professional edition
1. Create a Windows Service project
2. The project file directory created by default is as follows:
[optional] rename Service1 to MyService
a) Add an EventLog control to MyService Designer
The service1.cs code created by default includes the constructor, overloaded OnStart and overloaded OnStop functions. OnStart and OnStop functions will be triggered when starting and stopping the service respectively.
Paste the written code directly. There is no function here. It is to show that the service has been running through the event log and constantly writing a sentence to a text file.
using System; using System.Diagnostics; using System.IO; using System.ServiceProcess; using System.Text; using System.Timers; namespace FirstWindowsServiceApp { public partial class MyService : ServiceBase { public MyService() { InitializeComponent(); if (!System.Diagnostics.EventLog.SourceExists("MyEventSource")) { System.Diagnostics.EventLog.CreateEventSource("MyEventSource", "MyEventLog"); } //Event viewer details Provider Name property eventLog1.Source = "MyEventSource"; //The service log that will be seen in the event viewer eventLog1.Log = "MyEventLog"; } protected override void OnStart(string[] args) { eventLog1.WriteEntry("Personal Windows Service In OnStart"); //using System.Timers needs to be added; Pay attention to the command space //Using Timer to trigger text file writing at fixed intervals Timer timer = new Timer { Interval = 30000, //30 seconds Enabled = true }; timer.Elapsed += Timer_Elapsed; timer.Start(); } protected override void OnStop() { eventLog1.WriteEntry("Personal Windows Service In OnStop"); } private void Timer_Elapsed(object sender, ElapsedEventArgs e) { //You can see it in the event viewer EventData level event ID respectively eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId++); WriteToDisk(); } private void WriteToDisk() { string path = @"D:\Record.txt"; using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)) { string strContent = "MyWindwosServices: Service Info " + DateTime.Now.ToString() + "\n"; //Convert string to byte array byte[] byteFile = Encoding.UTF8.GetBytes(strContent); fs.Position = fs.Length; //Parameter: the data array to be written to the file, starting from the first few bytes of the array, how many bytes are written in total fs.Write(byteFile, 0, byteFile.Length); } } private int eventId = 0; } }
2. The next step is
Account represents the authentication method. Select LocalSystem here. If it is not set by default, the user name and password will be required when registering the service.
3. Registration service
Uninstallation corresponds to installutil /u "full path of executable application"
You can see the defined services in the service. After manual startup, the service starts running. You can use the event viewer and
This article is mainly used for getting started. It is relatively simple and mainly takes screenshots. If you have any questions in the process of practice, please communicate.