简体   繁体   中英

1053 windows service did not respond in timely fashion

I have one Web app running in IIS and communicates to a Windows Service through WCF hosted in the same machine. Internally Windows Service executes various job and sometimes it is quite busy with processing User requests. At 1st I had the issue with Start of Service because it some imes take much time to Start & eventually throws the Error ie Service Timeout. To fix this I moved the code to another Thread instead of the Main Thread. Everything works fine but now I observed that some times I am getting the error on Service Stop ie

**1053 windows service did not respond in timely fashion**

My Service OnStop() code:

protected override void OnStop()
        {
            // Stop the WCF service
            if (myServiceHost != null)
            {
                myServiceHost.Close();
                myServiceHost = null;
            }

            // Stop the scheduler

            if (myScheduleManager != null)
            {
                myScheduleManager.Stop();
            }

            // Update the registry value
            Logger.GetInstance().LogEvent(this.GetType().Name, LogLevel.INFO, "Updating registry...");
            Settings.GetInstance().LastStopTime = DateTime.Now.ToString();

            Logger.GetInstance().LogEvent(this.GetType().Name, LogLevel.INFO, "Service stopped.");
        }

It generally happens when Service is too busy. But finally it stops after 30-40 mins of time. I know this happens due to Time out issue in Windows Service coz the default time is very less & it treats it as Time out.

My question what is the best practice to avoid such bottle necks means shall I move my Stop related tasks under another Thread & do asynchronous call to release resources in that Thread. But what will happen if during release resources, User will Start the Service?

Just guessing, but perhaps it will help to track down the problem:

You have this myScheduleManager.Stop() which, (i guess) waits for processes to be finished. This is nice, but Windows only gives you 30 Seconds to execute ServiceBase.OnStop() .

If you are doing asynchronous processing here, you could perhaps implement the CancellationPattern by using the CancellationTokens from the .NET-Framework.

The easy approach probably you can identify to build a boolean type IsRunning in your myScheduleManager and determine during the OnStop()

if (myScheduleManager.IsRunning)
    myScheduleManager.Stop();

however, you have different threads finishing the processes which could over 30 seconds but you also can do abort your processes by applying

if (myScheduleManager.IsRunning && !_workerThread.Join(TimeSpan.FromSeconds(30))
    _workerThread.Abort 

myScheduleManager.Stop();

However, I didn't see the full code snippets so that's my guess to your scenario.

只是猜测但它可能有所帮助:确保将解决方案配置设置为Release而不是Debug。

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