diff --git a/.editorconfig b/.editorconfig index f7d2804..3f01883 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,6 +14,9 @@ insert_final_newline = true indent_size = 4 charset = utf-8-bom +# Don't show a message for analyzer suppressions that don't apply to a specific TFM, they probably apply to other TFMs +dotnet_diagnostic.IDE0079.severity = none + # XML project files [*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}] indent_size = 2 diff --git a/src/CenterEdge.Async/AsyncHelper.cs b/src/CenterEdge.Async/AsyncHelper.cs index 4fbea06..f883117 100644 --- a/src/CenterEdge.Async/AsyncHelper.cs +++ b/src/CenterEdge.Async/AsyncHelper.cs @@ -102,7 +102,9 @@ public static void RunSync(Func task) SynchronizationContext.SetSynchronizationContext(synch); try { +#pragma warning disable CA2012 var awaiter = task().GetAwaiter(); +#pragma warning restore CA2012 if (!awaiter.IsCompleted) { @@ -138,7 +140,9 @@ public static void RunSync(Func task, TState state) SynchronizationContext.SetSynchronizationContext(synch); try { +#pragma warning disable CA2012 var awaiter = task(state).GetAwaiter(); +#pragma warning restore CA2012 if (!awaiter.IsCompleted) { @@ -247,7 +251,9 @@ public static T RunSync(Func> task) SynchronizationContext.SetSynchronizationContext(synch); try { +#pragma warning disable CA2012 var awaiter = task().GetAwaiter(); +#pragma warning restore CA2012 if (!awaiter.IsCompleted) { @@ -284,7 +290,9 @@ public static T RunSync(Func> task, TState state SynchronizationContext.SetSynchronizationContext(synch); try { +#pragma warning disable CA2012 var awaiter = task(state).GetAwaiter(); +#pragma warning restore CA2012 if (!awaiter.IsCompleted) { @@ -305,16 +313,12 @@ public static T RunSync(Func> task, TState state } // Note: Sealing this class can help JIT make non-virtual method calls and inlined method calls for virtual methods - private sealed class ExclusiveSynchronizationContext : SynchronizationContext, IDisposable + private sealed class ExclusiveSynchronizationContext( + SynchronizationContext? parentSynchronizationContext) + : SynchronizationContext, IDisposable where TAwaiter : struct, ICriticalNotifyCompletion { - private readonly SynchronizationContext? _parentSynchronizationContext; - private readonly BlockingCollection<(SendOrPostCallback Callback, object? State)> _items = new(); - - public ExclusiveSynchronizationContext(SynchronizationContext? parentSynchronizationContext) - { - _parentSynchronizationContext = parentSynchronizationContext; - } + private readonly BlockingCollection<(SendOrPostCallback Callback, object? State)> _items = []; public override void Send(SendOrPostCallback d, object? state) { @@ -366,9 +370,9 @@ public void Run(TAwaiter awaiter) while (!_items.IsCompleted) { - var task = _items.Take(); + var (callback, state) = _items.Take(); - task.Callback(task.State); + callback(state); } } @@ -379,18 +383,18 @@ public void RunAlreadyComplete() while (!_items.IsCompleted) { - var task = _items.Take(); + var (callback, state) = _items.Take(); - ExecuteOnParent(task.Callback, task.State); + ExecuteOnParent(callback, state); } } // Executes a work item on the parent SynchronizationContext or on the thread pool if there is not one private void ExecuteOnParent(SendOrPostCallback callback, object? state) { - if (_parentSynchronizationContext != null) + if (parentSynchronizationContext != null) { - _parentSynchronizationContext.Post(callback, state); + parentSynchronizationContext.Post(callback, state); } else {