日期:2014-05-17  浏览次数:20855 次

Windows服务程序,前台不显示界面
本帖最后由 u011235865 于 2013-06-28 13:21:25 编辑
用C#写了一个定时监控服务,检测某一程序是否运行,如果没有运行,则启动改程序,如果已经运行,则不作任何操作。代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace RegularMonitoringProgram
{
    public partial class Service1 : ServiceBase
    {
        
        public Service1()
        {
            InitializeComponent();
            InitService();
        }
        private void InitService()
        {
            base.AutoLog = true;
            base.CanShutdown = true;
            base.CanStop = true;
            base.CanPauseAndContinue = true;
            base.ServiceName = "BMTService";  //这个名字很重要,设置不一致会产生 1083 错误哦!
        }
        protected override void OnStart(string[] args)
        {
            //timeExe 在Service1.Designer.cs 函数private void InitializeComponent()定义
            this.timeExe.Enabled = true;
            this.tCheckExe();
        }

        protected override void OnStop()
        { 
            this.timeExe.Enabled = false;
        }
        //定时执行的事件
        private void timeExe_Elapsed(object sender, System.Timers.ElapsedEventArgs e)