using System.Collections.Specialized; using System.Linq; using System.Threading; using System.Windows.Forms; using Winify.Utilities; namespace Winify { public partial class LogViewForm : Form { private readonly CancellationToken _cancellationToken; private readonly MainForm _mainForm; private readonly LogMemorySink _memorySink; public LogViewForm() { InitializeComponent(); } public LogViewForm(MainForm mainForm) : this() { _mainForm = mainForm; _mainForm.MemorySinkEnabled = true; } public LogViewForm(MainForm mainForm, LogMemorySink memorySink, CancellationToken cancellationToken) : this(mainForm) { _memorySink = memorySink; _memorySink.Events.CollectionChanged += Events_CollectionChanged; _cancellationToken = cancellationToken; } /// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && components != null) components.Dispose(); _memorySink.Events.CollectionChanged -= Events_CollectionChanged; _mainForm.MemorySinkEnabled = false; _memorySink.Clear(); base.Dispose(disposing); } private void Events_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: foreach (var item in e.NewItems) { var line = (string)item; textBox1.InvokeIfRequired(textbox => { textbox.AppendText(line); }); } break; } } private void LogViewForm_Load(object sender, System.EventArgs e) { Utilities.WindowState.FormTracker.Track(this); } } }