void BulkAddFilteredMsg(List <string> messages) { this.BeginInvoke(new MethodInvoker(delegate() { lock (locker) { LogBox.BeginUpdate(); LogBox.Items.AddRange(messages.ToArray()); GoToListBottom(); LogBox.EndUpdate(); WatchLogQueue.Clear(); } })); }
private void FilterBtn_Click(object sender, EventArgs e) { LogBox.Items.Clear(); // clear display. FilterWords.Clear(); // clear filter words. TotalLinesProcessed = 0; if (File.Exists(PathTxtBox.Text)) { // stop any old watches... StopWatchThread(); // clear anything pending. WatchLogQueue.Clear(); // generate new filter list. FilterWords = new List <string>(FilterTextBox.Text.Split('|')); for (int i = 0; i < FilterWords.Count; i++) { FilterWords[i] = FilterWords[i].Trim(); } // reopen file. if (streamReader != null) { streamReader.Close(); } // open new stream for reading and pump it all into the log data to parse. streamReader = new StreamReader(new FileStream(PathTxtBox.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, FileOptions.Asynchronous)); while (!streamReader.EndOfStream) { // add data to pending work. TryFilterLogMsg(streamReader.ReadLine()); } // now continue to watch it for new lines. StartWatchThread(); } // do a single insert for speed here. LogBox.BeginUpdate(); LogBox.Items.AddRange(WatchLogQueue.ToArray()); WatchLogQueue.Clear(); GoToListBottom(); LogBox.EndUpdate(); UpdateLineLabel(); }
void HandleFileWatch() { // setup a file watch FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); var wh = new AutoResetEvent(false); DirectoryInfo dir = new DirectoryInfo(PathTxtBox.Text).Parent; fileSystemWatcher.Path = dir.FullName; fileSystemWatcher.EnableRaisingEvents = true; fileSystemWatcher.Changed += (s, ev) => wh.Set(); // open a read only stream to tail it. var msg = ""; while (doWork) { try { if (streamReader == null) { doWork = false; break; } if (streamReader.BaseStream.CanRead) // is the file still open? { msg = streamReader.ReadLine(); // note: this was opened when we picked a file. we assume its valid here. } else { doWork = false; break; } } catch (Exception) { msg = "<<<< UNABLE TO PARSE LINE >>>>"; } if (msg != null) { LogBox.Invoke(new MethodInvoker(delegate() { lock (locker) { TryFilterLogMsg(msg); } Application.DoEvents(); })); } else { UpdateLineLabel(); { // batch update now. if (WatchLogQueue.Count > 0) { this.BeginInvoke(new MethodInvoker(delegate() { lock (locker) { LogBox.BeginUpdate(); LogBox.Items.AddRange(WatchLogQueue.ToArray()); if (LogBox.Items.Count > logLineLimit) { long reduceby = LogBox.Items.Count - logLineLimit; for (int i = 0; i < reduceby; i++) { LogBox.Items.RemoveAt(0); } } GoToListBottom(); LogBox.EndUpdate(); WatchLogQueue.Clear(); } })); } } wh.WaitOne(250); } } fileSystemWatcher.EnableRaisingEvents = false; // shut it down first. wh.Close(); WatchLogQueue.Clear(); }