In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the writing method of Windows Service service program in C#". In the daily operation, I believe that many people have doubts about the writing method of Windows Service service program in C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "what is the writing method of Windows Service service program in C#"! Next, please follow the editor to study!
Create a Windows Service project
Select the Windows Service (Windows Service) option from the Visual C# project, give the project a new file name, and then click OK.
Add function implementation
The OnStart function is executed when the service is started, and the OnStop function is executed when the service is stopped. Here, when you start and stop the service, write some text information to a text file, as follows:
Protected override void OnStart (string [] args) {FileStream fs = new FileStream (@ "d:\ test1.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter (fs); sw.BaseStream.Seek (0, SeekOrigin.End); sw.WriteLine ("WindowsService: Service Started" + DateTime.Now.ToString () + "\ n"); sw.Flush (); sw.Close (); fs.Close () } protected override void OnStop () {FileStream fs = new FileStream (@ "d:\ test1.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter (fs); sw.BaseStream.Seek (0, SeekOrigin.End); sw.WriteLine ("WindowsService: Service Stopped" + DateTime.Now.ToString () + "\ n"); sw.Flush (); sw.Close (); fs.Close ();} add installer
Go back to the design window and right-click to add the installer to generate the serviceInstaller1 and serviceProcessInstaller1 components. Rewrite the property ServiceName of serviceInstaller1 to your server name, set the startup mode to AUTOMATIC, and rewrite the property account of serviceProcessInstaller1 to LocalSystem.
Compile link generation service program
Build the project by selecting build from the build menu.
Installation service
Use the. Net framework tool INSTALLUTIL to install the service program.
Run InstallUtil.exe from the command line with the output of the project as an argument. Enter the following command on the command line:
C:\ Windows\ Microsoft.NET\ Framework\ v4.0.30319\ installutil myProject.exe
Start the service
Net start myService
Out of Service
Net stop myService
Uninstall the service
Run InstallUtil.exe from the command line with the output of the project as an argument.
C:\ Windows\ Microsoft.NET\ Framework\ v4.0.30319\ installutil / u myProject.exe
Debugging method of Windows Service Program
1) in Service1.cs, change the OnStart () method from the
Protected override void OnStart (string [] args)
Modify to
Public void OnStart ()
2) comment out the startup of the original service in the Main () function of Program.cs
ServiceBase [] ServicesToRun;ServicesToRun = new ServiceBase [] {new Service1 ()}; ServiceBase.Run (ServicesToRun)
Modify to
Service1 S1 = new Service1 (); s1.OnStart ()
Then set a breakpoint to start debugging. After debugging, change back to the above (1) and (2).
Windows service programs are used to perform scheduled tasks
General background service programs are often used for background operations such as system status monitoring, polling, timing tasks, and so on. For tasks that need to be executed at a fixed time or cycle, you can control the execution of tasks by setting timers.
Protected override void OnStart (string [] args) {/ / timer starts the method call timer1 = new System.Timers.Timer {Interval = 60000}; timer1.Elapsed + = new System.Timers.ElapsedEventHandler (MyTask); timer1.Enabled = true } private void MyTask (object sender, System.Timers.ElapsedEventArgs e) {/ / TODO: execute scheduled tasks} this is the end of the study on "what is the method of writing Windows Service service programs in C#". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.