From bb88552d64b95b7f832750d4d67505358f933d9e Mon Sep 17 00:00:00 2001 From: Robert Coltheart <13191652+robertcoltheart@users.noreply.github.com> Date: Fri, 17 Mar 2023 21:08:09 +1100 Subject: [PATCH] Only use the sync context for async delegates (#497) --- .../Runner/Impl/DelegateRunner.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Machine.Specifications.Core/Runner/Impl/DelegateRunner.cs b/src/Machine.Specifications.Core/Runner/Impl/DelegateRunner.cs index cb0e4cec..d55121e3 100644 --- a/src/Machine.Specifications.Core/Runner/Impl/DelegateRunner.cs +++ b/src/Machine.Specifications.Core/Runner/Impl/DelegateRunner.cs @@ -1,4 +1,6 @@ using System; +using System.Reflection; +using System.Runtime.CompilerServices; using System.Threading; namespace Machine.Specifications.Runner.Impl @@ -17,6 +19,13 @@ public DelegateRunner(Delegate target, params object[] args) public void Execute() { + if (!IsAsyncVoid()) + { + target.DynamicInvoke(args); + + return; + } + var currentContext = SynchronizationContext.Current; var context = new AsyncSynchronizationContext(currentContext); @@ -39,5 +48,11 @@ public void Execute() SynchronizationContext.SetSynchronizationContext(currentContext); } } + + private bool IsAsyncVoid() + { + return target.Method.ReturnType == typeof(void) && + target.Method.GetCustomAttribute() != null; + } } }