Problems with .NET in Windows XP

First, I use my own Process Wrapper to keep the original process path.

public class MCProcess() { public Process Process { get; set;} public string StartingPath { get; set;} public MCProcess(string start, Process p) { Process = p; StartingPath = start; } } 

Now I have a List<MCProcces> called runProcesses, which I use to track all the processes and start paths for every process that my program started.

Example:

 string path = "C:\\Windows\\System32\\notepad.exe"; Process temp = Process.Start(path); runningProcesses.Add(new MCProcess(path, temp)); 

Now, sometimes, I want to close the processes that I performed. Instead of looking at the task manager and trying to find MainModuleName for every process that I started, I included StartPath for some reason.

If I want to close the notepad, I just go through my running processes, find out which process has the initial notepad for the notepad, and then use Process.Kill to kill this process.

 string path = "C:\\Windows\\System32\\notepad.exe"; for (int i = 0; i < runningProcesses.Count; i++) { if (runningProcesses[i].StartingPath == path) { runningProcesses[i].Process.Kill(); runningProcesses.RemoveAt(i); } } 

This code works fine on Windows 7, and I had no problems at all. However, when using this on Windows XP, I get an ArgumentNullException with Process.Kill.

Is there something in the Process class that doesn't make it work well in Windows XP?

+4
source share
2 answers

I was struck by how this works in win 7. You modify the collection using it in a loop. You must maintain an index of the processes to be deleted, and then run a loop after that, delete all processes

Try something like

 var processesToRemove = runningProcesses.Where (p => String.Equals(p.StartingPath, path); foreach(var process in processToRemove) { process.Process.Kill(); runningProcesses.Remove(process); } 
+2
source

Try making your objects public:

 Public Process Process { get; set;} Public string StartingPath { get; set;} 

Then kill your process as follows:

 for (int i = 0; i < runningProcesses.Count; i++) { if (runningProcesses[i].StartingPath == path) { runningProcesses[i].Process.Kill(); } } 
0
source

Source: https://habr.com/ru/post/1445892/


All Articles