C# 如果在关闭时使用,是否保证方法的其余部分将在关闭后执行?
我有一个dispatcher对象,当请求通过管道进入我的服务时,它处理任务的启动和关闭 我的on-stop方法如下所示:C# 如果在关闭时使用,是否保证方法的其余部分将在关闭后执行?,c#,asynchronous,service,async-await,C#,Asynchronous,Service,Async Await,我有一个dispatcher对象,当请求通过管道进入我的服务时,它处理任务的启动和关闭 我的on-stop方法如下所示: protected override void OnStop() { try { Task t = CalculationServiceProcess.Instance.Shutdown(); t.Wait(); } catch (Aggre
protected override void OnStop()
{
try
{
Task t = CalculationServiceProcess.Instance.Shutdown();
t.Wait();
}
catch (AggregateException ae)
{
foreach (Exception e in ae.InnerExceptions)
{
Debug.WriteLine(e.Message);
}
}
}
我的Instance.Shutdown方法如下:
public Task Shutdown()
{
_shutdownTokenSource.Cancel();
Task t = _dispatcher.Shutdown();
_shutdownTokenSource.Dispose();
///some more code
return t;
}
dispatchers shutdown方法如下所示:
public Task Shutdown()
{
Task t = Task.WhenAll(_dispatchedItems.Select((item => item.Value.Task)).ToArray());
_dispatchedItems.Clear();
return t;
}
我想知道的是dispose方法会被调用吗?“//还有一些代码”会保证运行吗?或者,当所有任务完成时,我的t.Wait()方法是否会返回并在停止时完成执行,并且服务会在代码执行之前关闭?能否向“Instance.Shutdown”方法发布一些文档。这会有帮助的。@ShaunLuttin方法如问题所示。我不明白为什么它不能像你所希望的那样运行。是什么让你怀疑它不会正常工作?