SyncAsyncEventArgs.IsRunningSynchronously Property
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a value indicating whether the event handler was invoked synchronously or asynchronously. Please see SyncAsyncEventHandler<T> for more details.
public bool IsRunningSynchronously { get; }
member this.IsRunningSynchronously : bool
Public ReadOnly Property IsRunningSynchronously As Boolean
The same SyncAsyncEventHandler<T> event can be raised from both synchronous and asynchronous code paths depending on whether you're calling sync or async methods on a client. If you write an async handler but raise it from a sync method, the handler will be doing sync-over-async and may cause ThreadPool starvation. See Diagnosing .NET Core ThreadPool Starvation with PerfView for a detailed explanation of how that can cause ThreadPool starvation and serious performance problems.
You can use this IsRunningSynchronously property to check how the event is being raised and implement your handler accordingly. Here's an example handler that's safe to invoke from both sync and async code paths.
var client = new AlarmClient();
client.Ring += async (SyncAsyncEventArgs e) =>
{
if (e.IsRunningSynchronously)
{
Console.WriteLine("Wake up!");
}
else
{
await Console.Out.WriteLineAsync("Wake up!");
}
};
client.Snooze(); // sync call that blocks
await client.SnoozeAsync(); // async call that doesn't block
Product | Versions |
---|---|
Azure SDK for .NET | Latest |
Azure SDK for .NET feedback
Azure SDK for .NET is an open source project. Select a link to provide feedback: