namespace XFEEDToolBox.Core.Navigation; public sealed class FlightControlCleanupException(Exception primary,Exception cleanup):InvalidOperationException(primary.Message,primary) { public Exception CleanupFailure{get;}=cleanup; public override string ToString()=>base.ToString()+"\n释放控制时的附加错误:\n"+CleanupFailure; } public static class FlightControlCleanup { /// Independent owned controls must all be released, even when one release cannot be confirmed. public static async Task RunAllAsync(params Func[] releases) { List? failures=null; foreach(var release in releases) try{await release().ConfigureAwait(false);} catch(Exception error){(failures??=[]).Add(error);} if(failures is {Count:1})System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(failures[0]).Throw(); if(failures is {Count:>1})throw new AggregateException("Not all owned flight controls confirmed release.",failures); } public static async Task RunAsync(Func operation,Func release) { Exception? primary=null; try{await operation().ConfigureAwait(false);} catch(Exception error){primary=error;throw;} finally { try{await release().ConfigureAwait(false);} catch(Exception cleanup)when(primary is not null) { if(primary is OperationCanceledException){primary.Data["ControlReleaseFailure"]=cleanup.ToString();System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(primary).Throw();} throw new FlightControlCleanupException(primary,cleanup); } } } }