Feature Description: C # Creates a Windows service that D:\mcWindowsService.txt writes data when the service starts, and writes the current time every two seconds during the service run.
Principle these do not say, the three language two words are not clear, directly paste an example. Can't post pictures!! That compressed text inside is Word Document!! There's a picture of the truth
1. Create a blank Project
2. Add the references required to create the Windows service and select System.ServiceProcess.
3. Create the service class, inherit the ServiceBase, and the source code for the class is behind.
4. Add the installation class for the Windows service.
(1) Create a new view in the class name or solution:
(2) After the previous step will come out of the Class View, right-click to select the View Designer:
(3) Add Installer in Design view (May pop up warning box, without tube):
Service Creation Complete!
Installation and operation is needless to say!!
6 Service class source code ():
C # code
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Timers;
- namespace SR171
- {
- class Service17:System.ServiceProcess.ServiceBase
- {
- Public Service17 ()//Can be set by itself
- {
- this. ServiceName = "Myserviceforshowtime";
- this. CanStop = true;
- this. CanPauseAndContinue = true;
- this. AutoLog = true;
- #region Timer Events
- Timer Atimer = new timer (); //system.timers, not a form.
- atimer.elapsed + = new elapsedeventhandler (timedevent);
- Atimer.interval = 2 * 1000; number of seconds configured in//configuration file
- atimer.enabled = true;
- #endregion
- }
- Public Static void Main ()//must write
- {
- System.ServiceProcess.ServiceBase.Run (new Service17 ());
- }
- protected Override void OnStart (string[] args)//self-overwrite on request
- {
- FileStream fs = new FileStream (@"D:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write );
- StreamWriter m_streamwriter = new StreamWriter (FS);
- M_streamWriter.BaseStream.Seek (0, Seekorigin.end);
- M_streamwriter.writeline ("Mcwindowsservice:service Started" + DateTime.Now.ToString () + "\ n");
- M_streamwriter.flush ();
- M_streamwriter.close ();
- Fs. Close ();
- }
- protected Override void OnStop ()
- {
- FileStream fs = new FileStream (@"D:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write );
- StreamWriter m_streamwriter = new StreamWriter (FS);
- M_streamWriter.BaseStream.Seek (0, Seekorigin.end);
- M_streamwriter.writeline ("Mcwindowsservice:service Stopped" + DateTime.Now.ToString () + "\ n");
- M_streamwriter.flush ();
- M_streamwriter.close ();
- Fs. Close ();
- }
- Private Static void Timedevent (object Source, Elapsedeventargs E) //Runtime execution
- {
- FileStream fs = new FileStream (@"D:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write );
- StreamWriter m_streamwriter = new StreamWriter (FS);
- M_streamWriter.BaseStream.Seek (0, Seekorigin.end);
- M_streamwriter.writeline ("running.11." + DateTime.Now.ToString () + "\ n");
- M_streamwriter.flush ();
- M_streamwriter.close ();
- Fs. Close ();
- }
- }
- }
- C_ Create Windows Service collocation timer timer use instance (in code, version). RAR (462.8 KB)