简体   繁体   中英

Problems starting a .NET Windows Service when using Thread.Sleep

I have created a .NET Windows Service and installed the debug release from the bin/debug folder (yes I know this isn't a good approach, but I just want to be able to test it and attach debugger).

The service basically runs in an endless loop checking an FTP directory for files, processing them, sleeping for a minute and then looping.

When I attempt to start the service I get the following error

Error 1053: The service did not respond to the start or control request in a timely fashion

On further inspection the service is completing the first loop and then timing out during the first thread sleep. As such I'm somewhat confused as to how I should start the service. Is it my (lack of) understanding of threading that is causing this?

My start code is

protected override void OnStart(string[] args)
    {
        eventLog.WriteEntry("Service started");
        ThreadStart ts = new ThreadStart(ProcessFiles);
        workerThread = new Thread(ts);
        workerThread.Start();
    }

In the ProcessFiles function, once a loop has been completed I simply have

eventLog.WriteEntry("ProcessFiles Loop complete");
Thread.Sleep(new TimeSpan(0,1,0));

When I check the event logs the 'ProcessFiles Loop complete' log is there, but that is the last event before the service timesout and fails to start.

Can someone explain what I am doing wrong?

EDIT

The way I am handling the loop in my ProcessFiles function is as below

while (!this.serviceStopped)
{
    // Do Stuff
    eventLog.WriteEntry("ProcessFiles Loop complete");
    Thread.Sleep(new TimeSpan(0,1,0));
}

Cheers

Stewart

When I check the event logs the 'ProcessFiles Loop complete' log is there...

You might be having a code for file processing that does not return until the service gets timeout. You are trying to perform some task after interval, you better use System.Timers.Timer or System.Windows.Forms.Timer instead of loop to repeatedly perform some task.

To test if it is loop problem to can restrict loop to single iteration with sleep statement and check if service gets started.

protected override void OnStart(string[] args)
{
    aTimer = new System.Timers.Timer(10000);
    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    aTimer.Interval = 60000;
    aTimer.Enabled = true;
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    aTimer.Enabled = false;
    // Put file processing code here.
    aTimer.Enabled = true;
}

Doh. I just realised I had the following code in my Main Program method that I was using to be able to debug in VS. Obviously then when I installed the debug release it was putting an infinite timeout on the main thread. Removing the debug code fixed the issue.

#if DEBUG
            AdvanceLinkService myService = new AdvanceLinkService();
            myService.OnDebug();
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
        #else
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new AdvanceLinkService() 
            };
            ServiceBase.Run(ServicesToRun); 
        #endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM