C# 错误1053服务未响应启动或控制请求

C# 错误1053服务未响应启动或控制请求,c#,.net,service,C#,.net,Service,我正在从msdn链接学习窗口服务: 我已正确创建,第一次安装…当我尝试从Service.msc启动它时..抛出错误: 错误1053服务未响应启动或控制请求 这是我的代码: public partial class ASMSService : ServiceBase { private Timer myTimer; TimeSpan setTime; private DateTime previousDate; private DateTime todaysDa

我正在从msdn链接学习窗口服务:

我已正确创建,第一次安装…当我尝试从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

    }
}
这是设计代码

  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;
    }
部分类ASMS服务
{
///  
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域组件设计器生成的代码
///  
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.eventLog1=new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
// 
//ASMS服务
// 
this.ServiceName=“ASMSService”;
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
#端区
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);
        }
    }
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
静态void Main()
{
ServiceBase[]ServicesToRun;
ServicesToRun=新的ServiceBase[]
{ 
新ASMSService()
};
ServiceBase.Run(ServicesToRun);
}
}
我读过类似的文章。一些文章建议安装microsoft修补程序。另一些文章建议正确处理创建的对象。我也尝试在Onstop方法中这样做。但它不起作用。一些文章建议在Main()方法中调用ServiceBase.Run()方法。它也出现在我的代码中


请建议

我发现ASMS服务的计时器有一个很大的问题:

它创建于:

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());
}