This article is a mirror article of machine translation, please click here to jump to the original article.

View: 36585|Reply: 3

[ASP.NET] C# is a Windows service that executes tasks regularly

[Copy link]
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
Posted on 5/8/2019 10:59:16 AM |
Thank you for sharing, just in time, send it to me
Posted on 6/22/2020 2:04:33 PM |
Always report this mistake
 Landlord| Posted on 9/7/2020 3:21:59 PM |
awclovelin Posted on 2020-6-22 14:04
Always report this mistake

The command running is wrong, this service is not installed. If you are installing it by tapping the command line, you can refer to my next post.
The use of Topshelf for C# scheduled execution tasks
https://www.itsvse.com/thread-7503-1-1.html

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com