C
C#12mo ago
Play4K

❔ Event is called 6 times while the process got filtered down to parent process

private void ExecuteGridAddButtonCommand(object obj) { Process proc = obj as Process; Process[] procList = Process.GetProcessesByName(proc.ProcessName); List<Process> parentList = new List<Process>(); if (!BlockedProcesses.Contains(proc)) { foreach (var proc_ in procList) { try { var query = string.Format("SELECT ParentProcessId FROM Win32Process WHERE ProcessId = {0}", proc.Id); var search = new ManagementObjectSearcher("root\CIMV2", query); var results = search.Get().GetEnumerator(); results.MoveNext(); var queryObj = results.Current; var parentId = (uint)queryObj["ParentProcessId"]; var parent = Process.GetProcessById((int)parentId); parentList.Add(parent); } catch (Exception e) { continue; } } var filteredDuplicates = parentList.GroupBy(x => x.Id).Select(x => x.First()).ToList(); foreach (var p in filteredDuplicates) { ProcessInfo procInfo = new ProcessInfo(p.ProcessName + ".exe"); procInfo.Started += new Win32Process.ProcessInfo.StartedEventHandler(BlockedProcessStarted); procInfo.Terminated += new Win32Process.ProcessInfo.TerminatedEventHandler(BlockedProcessTerminated); BlockedProcesses.Add(p); break; } } } Note: in filteredDuplicates is only 1 process and its the parent process, but 6 events are called anyway..
5 Replies
arion
arion12mo ago
If "Discord" for this test example shows 6 events, ain't that intentional? Discord spawns multiple processes when it starts (feature of most browsers (and electron) these days) The parent process spawns all the child processes that are under its own care
Play4K
Play4K12mo ago
ok, but the main goal is to call only the event once.. even if there are mainy child processes Do you now how this could be achieved..?
arion
arion12mo ago
You want to be notified of only 1 of the events when it happens and not subsequent ones? If its the case, just unregister the respective event when it triggers.
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.