System.Diagnostics.Process.GetProcessesByName Question
-
Hi, I am opening a spreadsheet, then opening 10 others to extract information from them to update the 1st spreadsheet. I then want to kill them in the reverse order that they were opened. I know I can get an array of processes (See code below) but what order is the array? Do I walk the array in the reverse order to kill the last one first?
Process[] pProcess;
pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");Thank you,
Glenn
-
Hi, I am opening a spreadsheet, then opening 10 others to extract information from them to update the 1st spreadsheet. I then want to kill them in the reverse order that they were opened. I know I can get an array of processes (See code below) but what order is the array? Do I walk the array in the reverse order to kill the last one first?
Process[] pProcess;
pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");Thank you,
Glenn
Would it not be better to use the Office Object Model and open the workbooks from Excel? You would have more control over the process that way.
I know the language. I've read a book. - _Madmatt
-
Would it not be better to use the Office Object Model and open the workbooks from Excel? You would have more control over the process that way.
I know the language. I've read a book. - _Madmatt
Only problem is, even if you make sure and call
System.InteropServics.Runtime.Marshal.ReleaseComObject()
on each and every little object you access with the object model, and call all the cleanup functions of the object correctly, one still finds little EXCEL.EXE's hiding around when you open up Task Manager. I think it's maybe a COM Subsystem thing, where the system likes to leave the server EXE hanging around when you use COM objects from it -- even if they're properly released -- so that there's less latency the next time you make COM method calls from that same EXE. However, if you're cleaning up Excel every time your application is run and you don't want to have these little orphan instances of EXCEL.EXE hanging around, well, then you want to add this little piece of code back in to your cleanup method:foreach(System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("excel.exe"))
proc.Kill();It's not immediately obvious what order the processes come out in. I guess you'll just have to think of a way you can inspect each entry. In the meantime, though,
foreach
does in a pinch.Sincerely Yours, Brian Hart
-
Only problem is, even if you make sure and call
System.InteropServics.Runtime.Marshal.ReleaseComObject()
on each and every little object you access with the object model, and call all the cleanup functions of the object correctly, one still finds little EXCEL.EXE's hiding around when you open up Task Manager. I think it's maybe a COM Subsystem thing, where the system likes to leave the server EXE hanging around when you use COM objects from it -- even if they're properly released -- so that there's less latency the next time you make COM method calls from that same EXE. However, if you're cleaning up Excel every time your application is run and you don't want to have these little orphan instances of EXCEL.EXE hanging around, well, then you want to add this little piece of code back in to your cleanup method:foreach(System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("excel.exe"))
proc.Kill();It's not immediately obvious what order the processes come out in. I guess you'll just have to think of a way you can inspect each entry. In the meantime, though,
foreach
does in a pinch.Sincerely Yours, Brian Hart
I suspect that Mark isn't going to have to think too hard on this, but the OP might instead.
I'm not a stalker, I just know things. Oh by the way, you're out of milk.
Forgive your enemies - it messes with their heads
-
Hi, I am opening a spreadsheet, then opening 10 others to extract information from them to update the 1st spreadsheet. I then want to kill them in the reverse order that they were opened. I know I can get an array of processes (See code below) but what order is the array? Do I walk the array in the reverse order to kill the last one first?
Process[] pProcess;
pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");Thank you,
Glenn
-
Push your processes into a Stack and kill them when you're done in the same order you pop them from the Stack.