.NET实现可交互的WINDOWS服务的实例代码

2019-05-20 12:52:10刘景俊

        /// 修改此方法的内容。
        ///
privatevoid InitializeComponent()
        {
            //
            // Service1
            //
this.ServiceName ="JadeWatchService";

        }
        #endregion
        [STAThread]
        staticvoid Main()
        {
            System.ServiceProcess.ServiceBase.Run(new Service1());

        }
        ///
        /// 清理所有正在使用的资源。
        ///
protectedoverridevoid Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components !=null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        ///
        /// 设置具体的操作,以便服务可以执行它的工作。
        ///
protectedoverridevoid OnStart(string[] args)
        {
            threadForm =new Thread(new ThreadStart(FormShow));
            threadForm.Start();
        }

        ///
        /// 停止此服务。
        ///
protectedoverridevoid OnStop()
        {
            if (threadForm !=null)
            {
                if (threadForm.IsAlive)