Created
November 6, 2023 16:30
-
-
Save rahulsahay19/b2e0d304487db54949a551d85a708a06 to your computer and use it in GitHub Desktop.
AsyncAwaitWithTPLExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
public class AsyncAwaitWithTPLExample | |
{ | |
public static async Task Main(string[] args) | |
{ | |
Console.WriteLine("Main thread ID: " + Environment.CurrentManagedThreadId); | |
// Using async/await with TPL | |
int result = await Task.Run(() => | |
{ | |
Console.WriteLine("Task.Run thread ID: " + Environment.CurrentManagedThreadId); | |
// Simulate a CPU-bound operation | |
for (int i = 0; i < 1000000; i++) | |
{ | |
// Perform some heavy computation | |
Math.Sqrt(i); | |
} | |
return 42; // Return a result | |
}); | |
Console.WriteLine("Result: " + result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment