错误1053:服务未及时响应启动或控制请求

时间:2014-10-03 08:34:57

标签: c#

我的Windows服务突然无法正常工作,并开始提示错误1053。

这是我在c#

中的以下程序
using System;
using System.Net;
using System.IO;
using System.Data;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.ServiceProcess;
using System.Runtime.InteropServices;
using System.Diagnostics;
using EPocalipse.IFilter;
using System.Timers;
using System.Threading;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Net.Mail;
using System.Xml;

namespace MyWindowsService
{
    class Program : ServiceBase
    {

        System.Timers.Timer timer = new System.Timers.Timer();

        public static void Main(string[] args)
        {

            // If we are in debug mode, start it as a console app
            #if DEBUG
                Program instance = new Program();
                instance.OnStart(null);
                //Console.ReadLine();
                instance.OnStop();
            // If we are NOT in debug mode, start it as a service
            #else
                ServiceBase.Run(new Program());
            #endif
        }

        public Program()
        {
            CanPauseAndContinue = false;
            this.ServiceName = "MySciente Service 2012";
        }

        protected override void OnStart(string[] args)
        {

            base.OnStart(args);
            //System.Threading.Thread.Sleep(20 * 60 * 1000);
            startMyService();


        }

        protected override void OnStop()
        {
            base.OnStop();
            timer.Enabled = false;
            //sendMail();

        }

        private void InitializeComponent()
        {

        }

        private void startMyService()
        {
            fileMigration();

            timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer.Interval = (15 * 60 * 1000); //15 is minutes, so here is 15 minutes
            timer.Enabled = true;
        }
        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            fileMigration();
        }

        public static void fileMigration()
        {

            --My Program--

        }


    }
}

我可能知道它造成的原因是什么? Windows&反病毒更新我的服务从上个月15日导致此问题

1 个答案:

答案 0 :(得分:0)

显然fileMigration()花了太长时间。您不应该在服务启动期间执行繁重的操作。我不知道你提到的那些更新与这个问题之间的关系,这可能是巧合,或者反病毒现在在他们的文件系统挂钩中花费更多时间,导致fileMigration()花费更长时间。

如果您希望fileMigration()在启动时运行,请考虑使用TaskThreadPool.QueueUserWorkItem在单独的线程上运行它,同时尽早通知操作系统成功启动,即:

    private void startMyService()
    {
        Task.Factory.StartNew(fileMigration);

        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = (15 * 60 * 1000); //15 is minutes, so here is 15 minutes
        timer.Enabled = true;
    }