Examples of Windows services
Here is a test case, the service named Service1 Black is automatically generated, the red part of the code I added, green for me to add the comment, this case has no other meaning, just insert the record 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> The 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 } Main 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 is added to this process, please change the downstream To 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> Designer supports required methods-do not use the Code Editor Modify the contents 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.timer1_elapsed); // Service1 // This. ServiceName = "Service1"; ((System.ComponentModel.ISupportInitialize) (This.timer1)). EndInit (); } ///<summary> ///Clean up all the resources that are in use. ///</summary> protected override void Dispose (bool disposing) { if (disposing) { if (Components! = NULL) { components. Dispose (); } } base. Dispose (disposing); } ///<summary> ///set up a specific action 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 this service. ///</summary> protected override void OnStop () { //TODO: Add code here to perform the close operation required to stop the service. this.timer1.enabled = false; this. LogMessage ("Service Stopped"); } private void LogMessage (string xmsg) { Try { //Here is a record that inserts a message into the database, and below is the method by which I call the pre-written DB class to add records, or you can use other methods to write to the database. xmsg Db.querysql ("Insert into sysmsg (sysmsg) VALUES ('" +xmsg+ "')"); } Catch { //Do not take any action } } private void Timer1_elapsed (object sender, System.Timers.ElapsedEventArgs e) { logmessage ("Check service run! "); } } } |