Windows service example The following is a test case. The service name is service1 and the black part is automatically generated. The red part is the code I added and the green part is the annotation I added. This case has no other meaning, only insert records into the database.
Using system; Using system. collections; Using system. componentmodel; Using system. Data; Using system. diagnostics; Using system. serviceprocess; Using system. configuration. Install; Using sysdata. dB; Namespace servertest { Public class service1: system. serviceprocess. servicebase { Private system. Timers. Timer timer1; /// <Summary> /// Required designer variables. /// </Summary> Private system. componentmodel. Container components = NULL; Public service1 () { // This call is required by the windows. Forms component designer. Initializecomponent (); // Todo: add any initialization after the initcomponent call } // Master entry point of the process Static void main () { System. serviceprocess. servicebase [] servicestorun; // Multiple user services can be run in the same process. To // Add another service to this process. Change the downstream service. // Create another service object. For example, // // Servicestorun = new system. serviceprocess. servicebase [] {New service1 (), new myseconduserservice ()}; // Servicestorun = new system. serviceprocess. servicebase [] {New service1 ()}; System. serviceprocess. servicebase. Run (servicestorun ); } /// <Summary> /// The designer supports the required methods-do not use the code editor /// Modify the content of this method. /// </Summary> Private void initializecomponent () { This. timer1 = new system. Timers. Timer (); (System. componentmodel. isupportinitialize) (This. timer1). begininit (); // // Timer1 // This. timer1.enabled = true; This. timer1.interval = 30000; This. timer1.elapsed + = new system. Timers. elapsedeventhandler (this. timerw.elapsed ); // // Service1 // This. servicename = "service1 "; (System. componentmodel. isupportinitialize) (This. timer1). endinit (); } /// <Summary> /// Clear all resources in use. /// </Summary> Protected override void dispose (bool disposing) { If (disposing) { If (components! = NULL) { Components. Dispose (); } } Base. Dispose (disposing ); } /// <Summary> /// Set specific operations so that the service can perform its work. /// </Summary> Protected override void onstart (string [] ARGs) { // Todo: Add code here to start the service. This. timer1.enabled = true; This. logmessage ("service started "); } /// <Summary> /// Stop the service. /// </Summary> Protected override void onstop () { // Todo: Add code here to stop the service. This. timer1.enabled = false; This. logmessage ("Service stopped "); } Private void logmessage (string xmsg) { Try { // Insert a record with the Information xmsg to the database. below is the method for adding records to the database class that I wrote in advance. You can also use other methods to write records to the database. // DB. querysql ("insert into sysmsg (sysmsg) values ('" + xmsg + "')"); } Catch { // Do not perform any operations } } Private void timerincluelapsed (Object sender, system. Timers. elapsedeventargs E) { Logmessage ("Check service running! "); } } } |