Created
May 3, 2018 14:32
-
-
Save M-Yankov/9f3411bbe531c4a7745a5ccdfd15375a to your computer and use it in GitHub Desktop.
How to not block the UI thread
This file contains hidden or 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
/* 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) | |
{ | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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()
withTask.WhenAll()