user3767164
user3767164

Reputation: 161

Error 1053 service didnot respond to start or control request

I am learning window service from msdn link : http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx

I have properly created , install first Time ....when I try to start it from Service.msc ..it is throwing Error :

Error 1053 service didnot respond to start or control request

this is my Code :

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

this is Program class:

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

I have read similar posts on this ..Some posts suggest to install microsoft Patch ..Other suggest the object created should be disposed properly ..I also tried to do that in Onstop method..But it is not working ..Some posts suggest that ServiceBase.Run() method should be called in Main() method ...it is also present in My code

Please Suggest

Upvotes: 1

Views: 423

Answers (1)

Eugene Podskal
Eugene Podskal

Reputation: 10401

I can see one great problem with the ASMSService's timer:

It is created :

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

But it is never started:

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 method is required to start the timer if you use the simplest constructor

You may also want to read the following links, they are dealing with similar situations and provide few insights to consider:

Windows service with timer
System.Threading.Timer Not Starting?

Upvotes: 1

Related Questions