簡體   English   中英

IIS 7.5應用程序池回收不讓方法完成

[英]IIS 7.5 Application Pool Recycle not letting method finish

我有兩種方法:

public void Stop(bool immediate)
{
    Logger.Log(LogLevel.Debug, "Shutdown detected. Immediate: " + immediate);
    Shutdown();
    Logger.Log(LogLevel.Debug, "Unregistering");
    HostingEnvironment.UnregisterObject(this);
}

public void Shutdown()
{
    Logger.Log(LogLevel.Debug, "Preparing to stop UploadQueue");
    IsProcessing = false;

     //Set tasks to cancel to prevent queued tasks from parsing
     _cancellationTokenSource.Cancel();

     Logger.Log(LogLevel.Debug, "Waiting for " + _workerTasks.Count + " tasks to finish or cancel.");
     //Wait for tasks to finish
     Task.WaitAll(_workerTasks.Values.ToArray());

     Logger.Log(LogLevel.Debug, "Stopped UploadQueue");
}

該類使用IRegisteredObject接口來接收關閉通知。 在我的日志中,我得到了這個:

2014-07-18 15:30:55,913,DEBUG,Shutdown detected. Immediate: False
2014-07-18 15:30:55,913,DEBUG,Preparing to stop UploadQueue
2014-07-18 15:30:55,913,DEBUG,Waiting for 35 tasks to finish or cancel.
...
bunch of stuff
...
2014-07-18 15:31:28,471,DEBUG,Shutdown detected. Immediate: True
2014-07-18 15:31:28,471,DEBUG,Preparing to stop UploadQueue
2014-07-18 15:31:28,471,DEBUG,Waiting for 0 tasks to finish or cancel.
2014-07-18 15:31:28,471,DEBUG,Stopped UploadQueue
2014-07-18 15:31:28,471,DEBUG,Unregistering

為什么不進入Logger.Log(LogLevel.Debug, "Stopped UploadQueue"); 第一次? 它肯定似乎取消了任務並讓正在運行的任務完成。 (任務檢查它是否在運行前被取消,否則就是這樣)。

來自Task.WaitAll

AggregationException:

至少有一個Task實例被取消 - 或者 - 在執行至少一個Task實例期間拋出了異常。 如果任務被取消,則AggregateException在其InnerExceptions集合中包含OperationCanceledException。

您正在通過_cancellationTokenSource.Cancel();取消您的任務_cancellationTokenSource.Cancel(); 我認為這至少導致其中一個拋出異常。 您可能會在更高級別的堆棧框架中捕獲它並忽略它。 在try-catch塊中包裝Task.WaitAll

public void Shutdown()
{
    Logger.Log(LogLevel.Debug, "Preparing to stop UploadQueue");
    IsProcessing = false;

    //Set tasks to cancel to prevent queued tasks from parsing
    _cancellationTokenSource.Cancel();

    Logger.Log(LogLevel.Debug, "Waiting for " + _workerTasks.Count + " tasks to finish or cancel.");

     try
     {
         //Wait for tasks to finish
         Task.WaitAll(_workerTasks.Values.ToArray());

         Logger.Log(LogLevel.Debug, "Stopped UploadQueue");
     }
     catch (AggregationException e)
     {
         // Recover from the exception
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM