start and close instance of explorer.exe - c#

I have a strange behaviour when I try to start explorer.exe from c# like this:
ProcessStartInfo info = new ProcessStartInfo("explorer.exe", "E:");
info.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = info;
process.Start();
Thread.Sleep(2000);
bool res = process.CloseMainWindow(); // InvalidOperationException -> already exited
process.Close();
process.WaitForExit(5000);
The problem is:
the exception seems correct, because at this point HasExited returns already true. Nevertheless in the taskmanager the created instance of explorer is still present.
So I dont understand what my call does. I had thought it would directly start an instance of explorer, but it seems not or the explorer works in some different way.
And my second question: how can I start and shortly after that stop a new specific instance of explorer programmatically?
Edit
to answer some questions:
explorer option Launch Folder Windows in a separate process is set to true
the created process.Id is not present in taskmanager. For example: the new explorer instance shown in taskmanager has PID 4968 while the debugger shows 10752 as ID of the created (and exited) process.
Edit: here a screenshot from taskmanager after ~12 debug runs

This may be down to the fact that the explorer.exe process in question HAS exited. Windows does some strange things with multiple explorer windows and it depends on the options you have set. By default, all windows end up running in a single process if I remember correctly.
What I would do is output the processid for the process you just generated:
Console.WriteLine($"{process.Id} has exited {process.HasExited}");
Then look at task manager to see if you can find the corresponding process. I would imagine that the HasExited is true so you won't find the process, but the window will have opened.
You may have to set process.EnableRaisingEvents to true to get a valid answer from process.HasExited, I can't recall of the top of my head.
Also check the setting in Explorer via Folder Options to see if you have Launch Folder Windows in a separate process enabled or not on the view tab.
IF you do find your process, you can always kill off that process and see if your windows closes. If it does, then it may be that the explorer.exe is not creating a main window handle which you can check using Spy++
Edited with more info
Further more, #Hans Passant mentioned above that shell windows work different. So what actually happens is this, explorer.exe (1234) contacts the root explorer.exe (321), which in turn then creates a new window (if Launch separate is false) or spawns a subprocess explorer.exe (3445). Your process explorer.exe (1234) having done its job, then exits. No window is ever created by your process so CloseMainWindow() will not find a window to close and errors.
How to close a specific explorer window
To do so you need to utilise ShellWindows, see Is there a way to close a particular instance of explorer with C#?
For reference the code used there was:
ShellWindows _shellWindows = new SHDocVw.ShellWindows();
string processType;
foreach (InternetExplorer ie in _shellWindows)
{
//this parses the name of the process
processType = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
//this could also be used for IE windows with processType of "iexplore"
if (processType.Equals("explorer") && ie.LocationURL.Contains(#"C:/Users/Bob"))
{
ie.Quit();
}
}
Note, you need to be careful that you aren't closing a window the user wanted open in the first place. Is there a reason to close the window?

The problem is in the notion of has UI Interface, As per definition:
Closes a process that has a user interface by sending a close message
to its main window.
However explorer.exe is far more complicated than a simple process with UI.
If for example you use another, application, more simple (e.g Notepad), no exception will be raised:
ProcessStartInfo info = new ProcessStartInfo("notepad.exe");
info.WindowStyle = ProcessWindowStyle.Maximized;
Process process = new Process();
process.StartInfo = info;
process.Start();
Thread.Sleep(2000);
bool res = process.CloseMainWindow(); // InvalidOperationException -> already exited
process.Close();

Related

Open Edge in a new process and kill it again

I'm currently making a program, that essentially needs to open a link in Edge, take a screenshot and then close the browser again.
1st issue:
I can open the browser just fine, but it just opens a new tab instead of a new window, if the browser's already open.
I do not want to interfere with an already existing open Edge browser, that our users may be using, but instead open a completely new instance, take a screenshot and then close it again.
I tried using the following, with no luck - it still just opens a new tab
Process proc = new Process();
proc.StartInfo.FileName = "microsoftedge.exe";
proc.StartInfo.Arguments = "http://172.31.44.1/#/cameras" + " --new-window";
proc.Start();
2nd issue:
When trying to kill the process using proc.Kill() I end up getting a system.invalidoperationexception cannot process request because the process has exited , but the browser's still open
Any help is appreciated!
Thank you in advance
Check this out:
using System.Diagnostics;
Process proc = new Process();
proc.StartInfo.FileName = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
proc.StartInfo.Arguments = " --new-window http://google.com";
proc.Start();
It seems as though Chrome (and therefor also Edge) has changed the behavior of this recently.
It used to be the case that you could use the parameter "--no-service-autorun" to avoid the browser closing the original process, and to avoid you ending up with an invalid (dead) process id.
This doesn't work anymore.
When "Startup boost" is disabled in Edge, the first window does have the correct process id, but anything after that is still invalid.
The only thing that I could find that works, is to use "--user-data-dir" parameter to give each process it's own profile and process.
For example, use "msedge.exe --user-data-dir=C:\test123" (make sure each process has a unique directory).

Process.WaitForExit() waits on command window instead of actual application

I want the following code to run different applications and after they have openend additional tasks.
However some applications keep an command window open whilst others do not.
e.g. outlook.exe runs just fine,
ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Program Files\\Microsoft Office\\root\\Office16\\OUTLOOK.EXE");
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
But when running Visual Studio Code, the command window stays active in the background;
ProcessStartInfo startInfo = new ProcessStartInfo("C:\\Program Files (x86)\\Microsoft VS Code\\Code.exe");
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
It seems the loaded application process is the command window and not Visual Studio Code. So the command window disappears when I remove the WaitForExit(); But then i do not know when the application has loaded.
Is there another way to know if Visual Studio Code is fully loaded?
Process.WaitForExit() waits until the process you started has actually exited (as the name suggests). If this returns while the application you started is still visible, this is because the process you started is not used. For example, an existing instance of the Outlook process that is already running might be used.
If you want to wait for the process you start to be ready to accept user input, use Process.WaitForInputIdle().
Use WaitForInputIdle() to force the processing of your application to wait until the message loop has returned to the idle state. When a process with a user interface is executing, its message loop executes every time a Windows message is sent to the process by the operating system. The process then returns to the message loop. A process is said to be in an idle state when it is waiting for messages inside of a message loop. This state is useful, for example, when your application needs to wait for a starting process to finish creating its main window before the application communicates with that window.

Can't close down a process in C#

I am running an executable that opens a windows form from a webform. In visual studio the winform program runs a method and then closes the windows form correctly and shuts down the program. But when I run the same file as an executable it keeps the windows form open. I can see that this executable process is still running as SmartSheetAPI.exe.
When I check in properties the name of the file is "SmartSheetAPI.exe". If I end this process in the task manager it shuts down the windows form so I know that is the issue. However, I have tried using the below code on the webform to kill the process but again it doesn't work.
Process process = new Process();
process.StartInfo.FileName = #"P:\Visual Studio 2013\Projects\Smartsheet\SmartsheetAPI\obj\Debug\SmartSheetAPI.exe";
process.Start();
foreach (var processes in Process.GetProcessesByName("SmartSheetAPI.exe"))
{
process.Kill();
}
Does anyone know how to shut this thing down. As I say it works well in the SmartSheetAPI program in visual studio but doesn't shutdown the window as an executable. I just need to shut down this process once it has run the method.
EDIT:
The process that isn't closing is the vshost.exe and as such it is keep my application from closing for some reason (i.e. the windows form remains open). If I process.kill() this everything shuts down as required. However, the problem is that when I run the executable of that file the windows form stays open but I can't find the vshost.exe running to close it? I have disabled it and now the process that won't close is the SmartSheetAPI.exe file which is the program I am currently running. I just want to exit out of this program, but nothing I try seems to work.
After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited.
Process process = new Process();
process.StartInfo.FileName = #"P:\Visual Studio 2013\Projects\Smartsheet\SmartsheetAPI\obj\Debug\SmartSheetAPI.exe";
process.Start();
// do some stuff while process is alive ...
process.Kill();
process.WaitForExit();
// do stuff after the process has been killed ...
For more details see here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(v=vs.110).aspx
If your process cannot be killed it is probably waiting for something else. You might want to investigate that rather than forcing your application to be terminated.

How to close the windows opened by a process that has exited C#

I need to check if a process is launched and then verify its exit code.
EXProcess.StartInfo.FileName = strExE;
EXProcess.StartInfo.Arguments = tempFile;
EXProcess.Start();
do
{}while (!rdcProcess.WaitForExit(1000));
if (EXProcess.HasExited)
{ ...}
EXProcess.Kill();
Now I have to close the windows that were opened by this process programmatically. In the sense I have to terminate everything that was started by this process.
The process is launching an exe. I have to check if it was able to successfully launch it, so I need the exit code. Hence I'm waiting till the code exits. Now after I get the exit code, the window launched by the process stil exists. I have to clos that window/somehow kill the process. but EXProcess.Kill gives an exception - mentioned in the comment
How do I do it?
Please do help
I think that's what you're trying to acheive:
using (var process = new Process())
{
process.StartInfo.FileName = "notepad.exe";
process.StartInfo.Arguments = "";
process.Start();
process.WaitForExit(1000);
if (!process.HasExited)
process.Kill();
}
This opens notepad.exe, and kill it if it's running for more than 1 seconds. As you can see the notepad.exe window is closed.

Process.Kill() issue

I have the following lines of code:
Process aProcess = Process.Start("ieexplorer", aDummyHTMLFilePath);
//I do nothing in between
aProcess.Kill();
This runs smooth if there are NO other IE windows open.
But if there is a window open, I get a System.InvalidOperationException on aProcess.Kill(); saying :
Cannot process request because the process has exited.
Also, I notice that in this case, aProcess.HasExited is true right after line 1 in the code above.
How can I smoothly close IE, even if there are other IE windows open?
When you start a new instance of Internet Explorer like you do it will try to see if Internet Explorer is already running. If that is true the URL is opened in the already running instance and the new instance exits immediately. This means that when you try to kill the process you started it has already exited voluntarily. However, you will see a new browser window or tab on your screen but that is being hosted by the existing Internet Explorer process.
I didnt get the same issues as you
I found that the following worked if the process was still running or not
Process p = Process.Start("notepad.exe", "");
Thread.Sleep(5000);
if (!p.HasExited) p.Kill();
It only complained IF the window had been closed. Hence the check

Categories

Resources