SyncAsyncEventArgs.IsRunningSynchronously Property
Definition
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
Property Value
Remarks
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
Applies to
Azure SDK for .NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기