protected override void Execute(StreamClientContext context, CancellationTokenSource source) { // Create a task for each room. var tasks = new Task[context.Rooms.Length]; for (var index = 0; index < context.Rooms.Length; index++) { tasks[index] = Task.Factory.StartNew(obj => new StreamListener(_log).Listen(obj as StreamListenerContext, message => _inbox.Enqueue(message)).Wait(), new StreamListenerContext(_client, context.Rooms[index], context.Bot, source.Token), source.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current); } try { // Wait for any task to exit or a request to cancel. var index = Task.WaitAny(tasks, source.Token); if (!source.Token.IsCancellationRequested && index > 0 && index < context.Rooms.Length) { // Telling tasks to quit. _log.Information("The listener for {0} died.", context.Rooms[index]); _log.Information("Telling all listeners to cancel..."); source.Cancel(false); } } catch (OperationCanceledException) { _log.Information("Stream client was requested to stop."); if (!source.Token.IsCancellationRequested) { _log.Information("Telling all listeners to cancel..."); source.Cancel(false); } } WaitForTasksToStop(tasks); }
protected override void HandleCommand(MessageContext context, string[] args) { _inbox.Enqueue(new HelpEvent(context)); }