winservice1.cs

来自「《c#技术内幕代码》」· CS 代码 · 共 73 行

CS
73
字号
namespace CH7_16
{
    using System;
    using System.Collections;
    using System.Core;
    using System.ComponentModel;
    using System.Configuration;
    using System.Data;
    using System.Web.Services;
    using System.Diagnostics;
    using System.ServiceProcess;

    public class WinService1 : System.ServiceProcess.ServiceBase
    {
        /// <summary> 
        ///    Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components;
		private EventLog eventLog;

        public WinService1()
        {
            // This call is required by the WinForms Component Designer.
            InitializeComponent();

            string source = "Ch7_16";
            eventLog = new EventLog();
            eventLog.Source = source;
        }

        // The main entry point for the process
        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
    
            // More than one user Service may run within the same process. To add
            // another service to this process, change the following line to
            // create a second service object. For example,
            //
            //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new WinService1(), new MySecondUserService()};
            //
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);

        }

        /// <summary> 
        ///    Required method for Designer support - do not modify 
        ///    the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = "WinService1";
        }

        /// <summary>
        ///    Set things in motion so your service can do its work.
        /// </summary>
        protected override void OnStart(string[] args)
        {
			eventLog.WriteEntry("Ch7_16 starting up!");
        }
 
        /// <summary>
        ///    Stop this service.
        /// </summary>
        protected override void OnStop()
        {
			eventLog.WriteEntry("Ch7_16 shutting down!");
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?