Skip to content

Instantly share code, notes, and snippets.

@melcloud
Created May 28, 2017 09:44
Show Gist options
  • Save melcloud/0a7fe12217e7e2d53120df489e92abbd to your computer and use it in GitHub Desktop.
Save melcloud/0a7fe12217e7e2d53120df489e92abbd to your computer and use it in GitHub Desktop.
Async event loop in C#
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