C # create a windows service with a Timer to use the instance (using code, screenshot Version)

Source: Internet
Author: User

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

  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. IO;
  4. Using System. Linq;
  5. Using System. Text;
  6. Using System. Threading. Tasks;
  7. Using System. Timers;
  8. Namespace SR171
  9. {
  10. Class Service17: System. ServiceProcess. ServiceBase
  11. {
  12. Public Service17 () // you can set it by yourself.
  13. {
  14. This. ServiceName = "MyServiceForShowTime ";
  15. This. CanStop = true;
  16. This. CanPauseAndContinue = true;
  17. This. AutoLog = true;
  18.  
  19. # Region timer event
  20. Timer aTimer = new Timer (); // System. Timers, not form
  21. ATimer. Elapsed + = new ElapsedEventHandler (TimedEvent );
  22. ATimer. Interval = 2*1000; // number of seconds configured in the configuration file
  23. ATimer. Enabled = true;
  24. # Endregion
  25. }
  26. Public static void Main () // required
  27. {
  28. System. ServiceProcess. ServiceBase. Run (new Service17 ());
  29. }
  30. Protected override void OnStart (string [] args) // overwrite as required
  31. {
  32. FileStream fs = new FileStream (@ "d: \ mcWindowsService.txt", FileMode. OpenOrCreate, FileAccess. Write );
  33. StreamWriter m_streamWriter = new StreamWriter (fs );
  34. M_streamWriter.BaseStream.Seek (0, SeekOrigin. End );
  35. M_streamWriter.WriteLine ("mcWindowsService: Service Started" + DateTime. Now. ToString () + "\ n ");
  36. M_streamWriter.Flush ();
  37. M_streamWriter.Close ();
  38. Fs. Close ();
  39. }
  40. Protected override void OnStop ()
  41. {
  42. FileStream fs = new FileStream (@ "d: \ mcWindowsService.txt", FileMode. OpenOrCreate, FileAccess. Write );
  43. StreamWriter m_streamWriter = new StreamWriter (fs );
  44. M_streamWriter.BaseStream.Seek (0, SeekOrigin. End );
  45. M_streamWriter.WriteLine ("mcWindowsService: Service Stopped" + DateTime. Now. ToString () + "\ n ");
  46. M_streamWriter.Flush ();
  47. M_streamWriter.Close ();
  48. Fs. Close ();
  49. }
  50. Private static void TimedEvent (object source, ElapsedEventArgs e) // run
  51. {
  52. FileStream fs = new FileStream (@ "d: \ mcWindowsService.txt", FileMode. OpenOrCreate, FileAccess. Write );
  53. StreamWriter m_streamWriter = new StreamWriter (fs );
  54. M_streamWriter.BaseStream.Seek (0, SeekOrigin. End );
  55. M_streamWriter.WriteLine ("running.11.." + DateTime. Now. ToString () + "\ n ");
  56. M_streamWriter.Flush ();
  57. M_streamWriter.Close ();
  58. Fs. Close ();
  59. }
  60. }
  61. }
  • C_create a timer for Windows service setup. timeruse the timer (using code to do so, version. rar (462.8 KB)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.