Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write C#Windows Service Program

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to write C#Windows service programs, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

C#Windows service programming is a relatively advanced programming technology, which internally uses many core functions of the Windows operating system, but Microsoft .NET Framework has well encapsulated these technical details, which makes it very convenient for us to write our own Windows services using C #. The basic process is generally

Windows service programming 1. Create a C# project.

Create an EXE project, which can be in WinForm or command line format. Add references to System.ServiceProcess.dll and System.Configuration.Install.dll.

Windows service programming 2. Create a service class.

Add a new class that inherits the System.ServiceProcess.ServiceBase type, and set the ServiceName property in the constructor to indicate the name of the service. Then overload its OnStart method to respond to the event of starting the service, overload the OnStop method to respond to the event of stopping the service, overload the OnPause method to respond to the event of pausing the service, and overload the OnContinue method to respond to the event of resuming the service. When you overload these methods, you must return immediately, where there can be no long operations, and if the processing time is too long, the Windows service manager will feel that the Windows service stops responding and report an error. To do this, we can use a thread to do the actual work, while the OnStart method creates the thread, the OnStop method closes the thread, the OnPause method suspends the thread, and the OnContinue method resumes the running thread.

Windows service programming 3. Start the service.

Call "System.ServiceProcess.ServiceBase.Run (instance of custom service class)" in the main function to run the service. For example, "System.ServiceProcess.ServiceBase.Run (new MyService ())", where MyService is inherited from ServiceBase.

Windows service programming 4. Install the service.

A new class is added, which inherits from the System.Configuration.Install.Installer type, which is used to work with the installation command line tool InstallUtil.exe that comes with the Microsoft .NET Framework. We attach the System.ComponentModel.RunInstallerAttribute feature to this type and use the System.ServiceProcess.ServiceInstaller object and System.ServiceProcess.ServiceProcessInstaller object in its constructor to provide the system with installation information for the service. After the program is compiled, we can use the command line "InstallUtil.exe EXE file name" to register the service with the Windows service manager, and we can use the command line "InstallUtil.exe / u EXE file name" to log out the service from the Windows service manager.

Windows service programming 5. Write a service client.

This is an optional process according to the actual situation, because the Windows service has no user interface, so we can write a program with a user interface to display and control the data provided by the Windows service, and do some system settings and other operations. For example, for MS SQL Server, the database engine exists as a service, and SQL Server Enterprise Manager is a client software.

These are all the contents of the article "how to write C#Windows Service programs". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report