Skip to content

Instantly share code, notes, and snippets.

@M-Yankov
Created May 3, 2018 14:32
Show Gist options
  • Save M-Yankov/9f3411bbe531c4a7745a5ccdfd15375a to your computer and use it in GitHub Desktop.
Save M-Yankov/9f3411bbe531c4a7745a5ccdfd15375a to your computer and use it in GitHub Desktop.
How to not block the UI thread
/* How to not block UI thread: */
public static void Main()
{
/* Your code here */
Task exitTask = ExitCommandAsync();
Task.WaitAll(exitTask);
/* Console remains active */
}
private static Task ExitCommandAsync()
{
return Task.Run(() =>
{
while (Console.ReadLine() != ExitCommand)
{
}
});
}
@M-Yankov
Copy link
Author

M-Yankov commented Jan 3, 2019

Task.WaitAll() is blocking UI thread, but the idea is that it can be used like:
Task.WaitAll(exitTask, mainTask, backgroundTask1, backgroundTask2 ..... );

In order to see difference replace Task.WaitAll() with Task.WhenAll()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment