private static void RemoveProcessByProcessIDString(string procIDString) { WatchedProcess wp = null; lock (listlock) { if (!monitoredProcs.TryGetValue(procIDString, out wp)) { return; } monitoredProcs.Remove(procIDString); } }
private void tvMonProc_AfterSelect(object sender, TreeViewEventArgs e) { // preserve selected image icon tvMonProc.SelectedImageKey = (string)tvMonProc.Nodes[tvMonProc.SelectedNode.Index].Tag; TreeNode tn = tvMonProc.Nodes[tvMonProc.SelectedNode.Index]; WatchedProcess wp = null; if (!MonitoredProcesses.Processes.TryGetValue((string)tn.Tag, out wp)) { return; } // update enabled features checkboxes RefreshEnabledFeatures(NativeMethods.GetProcessFeatures((uint)wp.PID)); // update realtime log RealtimeLog.Pause(); // save off the currently displayed logs for the previously selected process if (lvRealtime.Items.Count != 0) { ListViewItem[] rgSavedtems = new ListViewItem[lvRealtime.Items.Count]; lvRealtime.Items.CopyTo(rgSavedtems, 0); perProcessRtLogs[(int)RealtimeLog.SelectedPid] = rgSavedtems; } // reload the previously displayed logs for the currently selected process lvRealtime.Items.Clear(); if (perProcessRtLogs.ContainsKey(wp.PID)) { ListViewItem[] rgSavedtems = perProcessRtLogs[wp.PID]; AddAlertListViewItemArray(rgSavedtems); } RealtimeLog.SelectedPid = (uint)wp.PID; RealtimeLog.Resume(); }
private static void RefreshProcesses(object param) { while (!terminateThread) { Thread.Sleep(1000); try { lock (listlock) { if (pauseUpdate) { continue; } } string[] procIDs = NativeMethods.GetProcessIDs(); if (procIDs == null || procIDs.Length == 0) { monitoredProcs.Clear(); continue; } List <string> removeKeys = new List <string>(); foreach (KeyValuePair <string, WatchedProcess> kvp in monitoredProcs) { bool found = false; foreach (string procID in procIDs) { if (String.CompareOrdinal(procID, kvp.Key) == 0) { found = true; break; } } if (!found) { removeKeys.Add(kvp.Key); } } if (removeKeys.Count != 0) { foreach (string s in removeKeys) { RemoveProcessByProcessIDString(s); } } WatchedProcess wp = null; foreach (string procID in procIDs) { if (!monitoredProcs.TryGetValue(procID, out wp)) { AddProcessByProcessIDString(procID); } } } catch { // blah } } }
private void RefreshProcesses(object sender, EventArgs args) { try { WatchedProcess wp = null; List <TreeNode> removeTn = new List <TreeNode>(); foreach (TreeNode tn in tvMonProc.Nodes) { if (!MonitoredProcesses.Processes.TryGetValue((string)tn.Tag, out wp)) { removeTn.Add(tn); break; } } if (removeTn.Count != 0) { foreach (TreeNode tn in removeTn) { tvMonProc.Nodes.Remove(tn); tvMonProc.ImageList.Images.RemoveByKey((string)tn.Tag); if (tn.Tag != null) { try { string strPid = (string)tn.Tag; int idx = strPid.IndexOf("]["); strPid = strPid.Substring(checked (idx + 2)); idx = strPid.IndexOf("]"); strPid = strPid.Substring(0, idx); int pid = int.Parse(strPid); if (perProcessRtLogs.ContainsKey(pid)) { perProcessRtLogs.Remove(pid); } } catch { continue; } } } } foreach (KeyValuePair <string, WatchedProcess> kvp in MonitoredProcesses.Processes) { bool found = false; TreeNode refTn = null; foreach (TreeNode tn in tvMonProc.Nodes) { if (String.Compare((string)tn.Tag, kvp.Key, true) == 0) { refTn = tn; found = true; break; } } string displayStr = null; string windowTitle = kvp.Value.MainWindowTitle; string strDesc = ""; if (kvp.Value.FileVersion == null || kvp.Value.FileVersion.FileDescription == null) { strDesc = "No description"; } else { strDesc = kvp.Value.FileVersion.FileDescription; } string strVer = null; if (kvp.Value.FileVersion == null || kvp.Value.FileVersion.FileVersion == null) { strVer = "No version"; } else { strVer = kvp.Value.FileVersion.FileVersion; } if (String.IsNullOrEmpty(kvp.Value.MainWindowTitle)) { displayStr = string.Format("{0} ({1})", strDesc, kvp.Value.ExecutableName); } else { int fitsChars = tvMonProc.Width / 7; if (windowTitle.Length >= fitsChars) { displayStr = windowTitle.Substring(0, fitsChars) + "..."; } else { displayStr = windowTitle; } } string tooltipStr = string.Format("Title: {0}\nDescription: {1}\nVersion: {2}\nProcess ID: {3}\n", kvp.Value.MainWindowTitle ?? "(no title)", strDesc, strVer, kvp.Value.PID); if (found) { if (String.Compare(refTn.Text, displayStr, true) != 0) { refTn.Text = displayStr; refTn.ToolTipText = tooltipStr; } } else { TreeNode tn = tvMonProc.Nodes.Add(displayStr); Icon icon = kvp.Value.AssociatedIcon; if (icon != null) { tvMonProc.ImageList.Images.Add(kvp.Key, icon); tn.ImageKey = kvp.Key; } tn.ToolTipText = tooltipStr; tn.Tag = kvp.Key; } } MonitoredProcesses.Resume(); } catch (Exception ex) { //MessageBox.Show(ex.ToString()); //throw; } }