Skip to content

Instantly share code, notes, and snippets.

@rahulsahay19
Created November 6, 2023 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsahay19/b2e0d304487db54949a551d85a708a06 to your computer and use it in GitHub Desktop.
Save rahulsahay19/b2e0d304487db54949a551d85a708a06 to your computer and use it in GitHub Desktop.
AsyncAwaitWithTPLExample
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