|
|
Posted on 4/27/2019 5:56:09 PM
|
|
|
|

preface
Following on from the previous article on Windows Task Scheduler, this article will record the creation of Windows services.
Environment & Tools
>Windows 10 system
>VS 2017
Text
1: Create a new solution and create a Windows service. The name is "WindowsServiceDemo.exe”
2: Double-click on the "Service1.cs" file, right-click on the blank space – > select "Add installer".
3: In the two components that are generated, click on the "serviceProcessInstaller1" component, and in the properties form, set the "Account" account type to "LocalSystem".
4: In the two components generated, click the "serviceInstaller1" component, in the properties form, set the corresponding properties, and then save.
5: Right-click on "Service1.cs" – > select "View Code".
6: In the corresponding method, write the processing logic. OnStart() method: refers to the method that enters when the Windows service starts. OnStop(): As the name suggests, this is the way to enter when the service stops. We can write a timer to let the program process the logic at regular intercourse.
7: Create two notepad files, name and modify the suffix names "Install.bat" and "Uninstall.bat" respectively Note: The suffix name is .bat. Paste it inside the project. And change the attribute to "Copy to local".
8: The two files we created just now are for installing the service and uninstalling the service. Now write the batch code to these two files and save. Open the "Install.bat" file and write the following code
Note: "WindowsServiceDemo.exe" is the exe program just generated. "AServiceTest" is the "ServiceName" set in the "serviceInstaller1" component.
Open the "Uninstall.bat" file and write the following code
9: F6 generates it. In the project "bin\Debug" directory. Right-click on "Install.bat" – > select "Run as administrator".
Once the service installation is complete. We can see that the "Log.txt" file writes a line of data every 10 seconds.
We can see the installed services. Run – > enter "services.msc" enter.
Stop service now. Right-click on "Uninstall.bat" – > select "Run as administrator".
After the service is uninstalled. We can see that the "Log.txt" file also has a line of data written to it.
#Windows service debugging
Windows services cannot be started directly with F5, F10 debugged. So how to debug the program if there is an abnormality? 1: First of all, use the administrator to run the "Install.bat" file and let the program run first.
2: Go back to VS and select "Debug" in the menu bar – > select "Attach to Process".
3: In the pop-up form, check "Show all users' processes", find the .exe application we generated, and click "Attach".
4: Break the point in the method, so that when it runs for a certain time, it can trigger debugging.
Epilogue
Windows service debugging is very troublesome, sometimes the program is written in a problem, and the service cannot start, which makes it more difficult to debug. Generally, writing Windows services will be accompanied by a console application. Debug it in the console application, and then move it to Windows Services. The next topic will describe a simpler way to create a Windows service.
Note: The batch files launched and uninstalled by the two programs must be run as administrators. Otherwise, it will be incorrect.
|
Previous:Common Algorithms for Sharing Big Data (Applications)Next:The use of Topshelf for C# scheduled execution tasks
|