Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12141

In C#, is it possible to know if the method is being executed in async flow?

$
0
0

Can we have a method IsInAsyncContext that identifies if the control is inside an async execution flow or not? Check the following usage.

public static void Main(){    IsInAsyncContext(); // Should return false.    AsyncMethod().GetAwaiter().GetResult();    SyncMethod();}private static async Task AsyncMethod(){    IsInAsyncContext(); // Should return true.    SyncMethod();}private static void SyncMethod(){    // Should return false if called from Main, true if called from AsyncMethod.    IsInAsyncContext();}

Viewing all articles
Browse latest Browse all 12141

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>