Skip to content

Commit

Permalink
Only use the sync context for async delegates (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertcoltheart authored Mar 17, 2023
1 parent 3f7b369 commit bb88552
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Machine.Specifications.Core/Runner/Impl/DelegateRunner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Machine.Specifications.Runner.Impl
Expand All @@ -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);
Expand All @@ -39,5 +48,11 @@ public void Execute()
SynchronizationContext.SetSynchronizationContext(currentContext);
}
}

private bool IsAsyncVoid()
{
return target.Method.ReturnType == typeof(void) &&
target.Method.GetCustomAttribute<AsyncStateMachineAttribute>() != null;
}
}
}

0 comments on commit bb88552

Please sign in to comment.