Created
May 28, 2017 09:44
-
-
Save melcloud/0a7fe12217e7e2d53120df489e92abbd to your computer and use it in GitHub Desktop.
Async event loop in C#
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
private static async Task EventLoop(BufferBlock<object> bufferBlock, CancellationToken cancellationToken) | |
{ | |
while (true) | |
{ | |
object msg; | |
try | |
{ | |
msg = await bufferBlock.ReceiveAsync(TimeSpan.FromSeconds(3), cancellationToken); | |
} | |
catch (TimeoutException) | |
{ | |
NoMessagesInTimeout(); | |
continue; | |
} | |
catch (Exception e) | |
{ | |
break; | |
} | |
ProcessMessage(msg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment