void QueueWatcher() { _logFile.Create().Close(); while (_threadRunning) { if (_logQueue.Count > 0) { _watcherThread.IsBackground = false; using (var f = _logFile.AppendText()) { while (_logQueue.Count > 0) { var d = _logQueue.Dequeue(); if (d.Message == oldLog.Message) { return; } oldLog = d; f.WriteLine(d.Message); Console.ForegroundColor = GetConsoleColour(d.WarningLevel); Console.WriteLine(d.Message); Console.ResetColor(); } } _watcherThread.IsBackground = true; } Thread.Sleep(5); } }
/// <summary> /// Log a message to message box /// </summary> /// <param name="message"></param> /// <param name="type"></param> public void Log(string message, MessageType type = MessageType.WARNING, object tag = null) { ListViewItem toAdd = new ListViewItem(message); mList.SmallImageList = imageList; toAdd.Tag = tag; if (type == MessageType.ERROR) { toAdd.ImageIndex = 0; } else if (type == MessageType.MESSAGE) { toAdd.ImageIndex = 2; } else { toAdd.ImageIndex = 1; } // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.mList.InvokeRequired) { if (this.mList.IsDisposed == false) { logMessage func = new logMessage(Log); this.Invoke(func, new object[] { message, type, tag }); } } else { mList.Items.Add(toAdd); } }