错误1053服务没有响应启动或控制请求

本文关键字:启动 控制 请求 响应 1053 服务 错误 | 更新日期: 2023-09-27 18:10:34

我正在从msdn学习窗口服务link:http://msdn.microsoft.com/en-us/library/zt39148a (v = vs.110) . aspx

我已经正确创建,第一次安装....当我试图从Service启动它时。msc . .抛出错误:

错误1053服务没有响应启动或控制请求

这是我的代码

public partial class ASMSService : ServiceBase
 {
    private Timer myTimer;
    TimeSpan setTime;
    private DateTime previousDate;
    private DateTime todaysDate;
    public ASMSService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";
    }
    protected override void OnStart(string[] args)
    {
        myTimer = new System.Threading.Timer(new TimerCallback(TimerAction1));
        SetTimer(11, 07, 00);
    }
    protected override void OnStop()
    {
    }
    private void SetTimer(int hours, int minutes, int seconds)
    {
        todaysDate = DateTime.Today;
        previousDate = todaysDate.AddDays(-1);
        setTime = todaysDate.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds).TimeOfDay; ;
    }
    private void TimerAction1(object e)
    {
 //some Code
    }
}

this is design Code

  partial class ASMSService
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.eventLog1 = new System.Diagnostics.EventLog();
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
            // 
            // ASMSService
            // 
            this.ServiceName = "ASMSService";
            ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
        }
        #endregion
        private System.Diagnostics.EventLog eventLog1;
    }

这是程序类:

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new ASMSService() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }

我看过类似的帖子。一些帖子建议安装微软补丁…还有人建议创建的对象应该妥善处理。我也试着在Onstop方法中这样做,但它不起作用。一些帖子建议ServiceBase.Run()方法应该在Main()方法中调用…它也出现在我的代码

请建议

错误1053服务没有响应启动或控制请求

我可以看到ASMSService的计时器有一个很大的问题:

它被创建:

myTimer = new System.Threading.Timer(new TimerCallback(TimerAction1));

但是它从来没有开始过:

private void SetTimer(int hours, int minutes, int seconds)
{
    todaysDate = DateTime.Today;
    previousDate = todaysDate.AddDays(-1);
    setTime = todaysDate.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds).TimeOfDay; ;
    // You have set the setTime field, otherwise the timer will still have the infinite dueTime and interval - it is not running at all
    // You should call SetChange method to start it.
    this.mytimer.SetChange(0, (Int64)setTime.ToMilliseconds());
}
如果使用最简单的构造函数 ,则需要

SetChange方法来启动计时器。

你可能也想看看下面的链接,它们都在处理类似的情况,并提供了一些值得考虑的见解:

Windows service with timer
System.Threading.Timer未启动?