Windows服务无法正常运行

时间:2017-03-24 13:29:19

标签: c# service

我希望有人可以帮助我。

我有一个小应用程序在PC上运行,具有管理员权限。使用相同的代码,它100%工作

现在我需要将此设置作为一项服务,尽管我已设法将其转换为服务,但我已成功安装。但它永远不会发火。

这是我写的第一个服务,我不确定我是否在此过程中犯了一些重大错误,因为我不确定如何调试它们,因为每次启动都说我需要先安装它。

该服务必须监控特定进程,记事本进行测试。如果它发现它打开等待10秒并再次寻找它。如果它再次发现它必须杀死进程。这整个过程必须每隔5到10分钟完成一次。

我用的代码是。该服务在timer1和timer 2上有两个定时器

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace testservice
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1.Interval = 10000;
            timer1.Start();
        }

        protected override void OnStop()
        {
            timer1.Stop();
            timer2.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            Checking();
        }
        private void Checking()
        {
            Process[] pname = Process.GetProcessesByName("Notepad");
            if (pname.Length == 0)
            {
                timer1.Stop();
                timer1.Interval = 10000;
                timer1.Start();
                GC.Collect();

            }
                //MessageBox.Show("nothing");
            else
            {
                timer1.Stop();
                timer1.Interval = 10000;
                timer2.Stop();
                timer2.Interval = 10000;
                timer2.Start();
                GC.Collect();
            }
                //MessageBox.Show("run");
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            Process[] pname = Process.GetProcessesByName("Notepad");
            if (pname.Length == 0)
            {
                timer2.Stop();
                timer1.Interval = 10000;
                timer1.Start();
            }
            //MessageBox.Show("nothing");
            else
            {
                foreach(var process in Process.GetProcessesByName("Notepad"))
                {
                    process.Kill();
                }
                timer1.Interval = 10000;
                timer1.Start();
                System.GC.Collect();
            }
        }
    }
}

为什么这可以作为应用而不是服务?

1 个答案:

答案 0 :(得分:0)

我最终做的是以下内容,它仍然可能不是正确的方法,但确实有效。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace AveryMonitor
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
        private  System.Timers.Timer timer1;

        private static int check;

        protected override void  OnStart(string[] args)
        {
            this.timer1 = new System.Timers.Timer(15000);

            this.timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            this.timer1.Enabled = true;
            this.timer1.Start();

        }
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            Process[] pname = Process.GetProcessesByName("Notepad");
            if (pname.Length == 0)
            {
                check = 0;
            }
            //MessageBox.Show("nothing");
            else
            {
                if (check == 1)
                {

                    foreach (var process in Process.GetProcessesByName("Notepad"))
                    {
                        process.Kill();
                    }
                    check = 0;
                }
                else
                {
                    check = 1;
                }
            }
        }

        protected override void OnStop()
        {
            timer1.Stop();

            timer1.Enabled = false;

        }
    }
}
相关问题