Function Description: C # creates a windows service. when the service is started, D: \ mcWindowsService.txt writes data, and the current time is written every two seconds during service running.
The principle of this will not be said, three words and two words are unclear, directly paste an instance. Images cannot be pasted !! The compressed file contains a Word document !! Images and truth
1. Create a blank Project
2. Add a reference required to create a windows Service and select System. ServiceProcess.
3. Create a service class and inherit from ServiceBase. the source code of the class is later.
4. Add the installation class for windows Services.
(1) create a view in the class name or solution:
(2) the class view will be displayed after the previous step. Right-click and choose View designer:
(3) Add the installer in the design view (A warning box may pop up, so you don't need to worry about it ):
Service created!
Install and run it !!
6. Service 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 () // you can set it by yourself.
- {
- This. ServiceName = "MyServiceForShowTime ";
- This. CanStop = true;
- This. CanPauseAndContinue = true;
- This. AutoLog = true;
-
- # Region timer event
- Timer aTimer = new Timer (); // System. Timers, not form
- ATimer. Elapsed + = new ElapsedEventHandler (TimedEvent );
- ATimer. Interval = 2*1000; // number of seconds configured in the configuration file
- ATimer. Enabled = true;
- # Endregion
- }
- Public static void Main () // required
- {
- System. ServiceProcess. ServiceBase. Run (new Service17 ());
- }
- Protected override void OnStart (string [] args) // overwrite as required
- {
- 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) // run
- {
- 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 a timer for Windows service setup. timeruse the timer (using code to do so, version. rar (462.8 KB)