/// <summary> /// add an action to queue that will append the text /// </summary> /// <param name="format">string format</param> /// <param name="args">format args, if any.</param> protected virtual void PerformWriteLine(string format, params object[] args) { try { _collection.Add(Execute); } catch (Exception ex) { Console.Error.WriteLineAsync(ex.ToString()); throw; } void Execute() { string toBeWritten = GetString(); try { OutputHelperAppendedToEventArgs e = new OutputHelperAppendedToEventArgs(toBeWritten); _helper.WriteLine(toBeWritten); OnTextAppended(e); } catch (Exception e) { Console.Error.WriteLineAsync(e.ToString()); throw; } } string GetString() { if (format == null) { return(string.Empty); } if (args == null) { return(format); } try { return(string.Format(format, args)); } catch (FormatException ex) { return($"The format string [{format}] threw a format exception: [{ex}]."); } } }
/// <summary> /// Event invocator for <see cref="TextAppended"/> <see langword="event"/> /// </summary> /// <param name="e">event arguments</param> protected virtual void OnTextAppended(OutputHelperAppendedToEventArgs e) { if (e != null) { try { _outputHelperExecutor.EnqueueAction(() => TextAppended?.Invoke(this, e)); } catch (Exception exception) { Console.WriteLine(exception); throw; } } }