Created
August 12, 2015 15:18
-
-
Save punitganshani/3d51bd906fe746c2e9e8 to your computer and use it in GitHub Desktop.
AsyncContext for Console Applications - Static Main
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
public class AsyncContext | |
{ | |
public static void Run(params Func<Task>[] tasks) | |
{ | |
if (tasks == null) | |
return; | |
Task[] asyncTasks = new Task[tasks.Length]; | |
int counter = 0; | |
foreach (var task in tasks) | |
{ | |
asyncTasks[counter++] = Task.Factory.StartNew<Task>(async () => { await task(); }); | |
} | |
Task.WaitAll(asyncTasks); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
AsyncContext.Run(DoSomethingAsync); | |
} | |
private static async Task DoSomethingAsync() | |
{ | |
// async operation here | |
var result = await MyOperationAsync(); | |
var workCompleted = result.GetCompleteJson(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment