private void OnButtonRunClick(object sender, EventArgs e) { _richTextBox.Clear(); _cancellationTokenSource = new CancellationTokenSource(); _engine = new HackerEngine(_cancellationTokenSource, _trackBarPassworLength.Value); SetControlsEnabled(false); _engine.Run(); Task.Factory.StartNew(() => { foreach (var message in _engine.MessagePipe.GetConsumingEnumerable()) { _richTextBox.AppendText(message); _richTextBox.AppendText(Environment.NewLine); Application.DoEvents(); } }, _cancellationTokenSource.Token, TaskCreationOptions.PreferFairness, TaskScheduler.FromCurrentSynchronizationContext()) .ContinueWith(x => { SetControlsEnabled(true); }, TaskScheduler.FromCurrentSynchronizationContext()); }