C # Create Windows service collocation timer timer use instance (in code, screenshot version)

Source: Internet
Author: User

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
  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 ()//Can be set by itself
  13. {
  14. this. ServiceName = "Myserviceforshowtime";
  15. this. CanStop = true;
  16. this. CanPauseAndContinue = true;
  17. this. AutoLog = true;
  18. #region Timer Events
  19. Timer Atimer = new timer (); //system.timers, not a form.
  20. atimer.elapsed + = new elapsedeventhandler (timedevent);
  21. Atimer.interval = 2 * 1000; number of seconds configured in//configuration file
  22. atimer.enabled = true;
  23. #endregion
  24. }
  25. Public Static void Main ()//must write
  26. {
  27. System.ServiceProcess.ServiceBase.Run (new Service17 ());
  28. }
  29. protected Override void OnStart (string[] args)//self-overwrite on request
  30. {
  31. FileStream fs = new FileStream (@"D:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write );
  32. StreamWriter m_streamwriter = new StreamWriter (FS);
  33. M_streamWriter.BaseStream.Seek (0, Seekorigin.end);
  34. M_streamwriter.writeline ("Mcwindowsservice:service Started" + DateTime.Now.ToString () + "\ n");
  35. M_streamwriter.flush ();
  36. M_streamwriter.close ();
  37. Fs. Close ();
  38. }
  39. protected Override void OnStop ()
  40. {
  41. FileStream fs = new FileStream (@"D:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write );
  42. StreamWriter m_streamwriter = new StreamWriter (FS);
  43. M_streamWriter.BaseStream.Seek (0, Seekorigin.end);
  44. M_streamwriter.writeline ("Mcwindowsservice:service Stopped" + DateTime.Now.ToString () + "\ n");
  45. M_streamwriter.flush ();
  46. M_streamwriter.close ();
  47. Fs. Close ();
  48. }
  49. Private Static void Timedevent (object Source, Elapsedeventargs E) //Runtime execution
  50. {
  51. FileStream fs = new FileStream (@"D:\mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write );
  52. StreamWriter m_streamwriter = new StreamWriter (FS);
  53. M_streamWriter.BaseStream.Seek (0, Seekorigin.end);
  54. M_streamwriter.writeline ("running.11." + DateTime.Now.ToString () + "\ n");
  55. M_streamwriter.flush ();
  56. M_streamwriter.close ();
  57. Fs. Close ();
  58. }
  59. }
  60. }
    • C_ Create Windows Service collocation timer timer use instance (in code, version). RAR (462.8 KB)

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.