Created
January 16, 2017 08:24
-
-
Save DanielLoth/0e0bd1fe482e675670886760c29c9c12 to your computer and use it in GitHub Desktop.
AsyncEx.Context question (dotnet core)
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 Nito.AsyncEx; | |
using System; | |
using System.Threading.Tasks; | |
namespace AsyncEx.Context.ConsoleApp | |
{ | |
public class Program | |
{ | |
public static int Main(string[] args) | |
{ | |
Console.WriteLine("Hello, world!"); // Main thread | |
var result = AsyncContext.Run(() => MainAsync(args)); // Main thread | |
return result; // Main thread, worker threads exist | |
} | |
private static async Task<int> MainAsync(string[] args) | |
{ | |
await Task.Delay(100); // Main thread | |
// Executes on main thread, but worker threads exist from | |
// this point onwards until the program terminates. | |
var task = AsyncInMethodSignature(); | |
var someMoreWork = 10; | |
await NoAsyncInMethodSignature(); | |
await task; | |
return someMoreWork; | |
} | |
static Task NoAsyncInMethodSignature() | |
{ | |
// This method executes on main thread | |
return Task.CompletedTask; | |
} | |
static async Task<int> AsyncInMethodSignature() | |
{ | |
// This method executes on main thread | |
await Task.Delay(100); | |
return await Task.FromResult(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment