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.
Related
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.
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();
I'm using external exe file in my C# code.
please check below code.
This code I just find some web site.
string str = #"C:\joseph_Test\external_vendor.exe";
Process myprocess = new Process();
myprocess.StartInfo.FileName = str;
myprocess.Start();
I checked this code is working, and no problem, but next test I need to use another different type of external exe file.
and I aware that previous 'myprocess' is still alive.
even I restart program, myprocess is still alive.
So I search internet that how to kill or terminate existing process.
weird thing is that myprocess never terminated.
First, I tried the code below but the process is still alive.
myprocess.WaitForExit();
myprocess.Close();
I used the link below, but it is still not killed.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/50ecbcf2-d2d3-4f21-9775-5b8be1bd4346/how-to-terminate-a-process-in-c?forum=csharpgeneral
Also not terminated with below way.
Kill some processes by .exe file name
I suspect this exe file made by vendor so this happen caused.
But the theory is C# create process instance so I believe C# could terminate or kill this process.
Using the process as you have created:
Process myprocess = new Process();
myprocess.StartInfo.FileName = #"C:\joseph_Test\external_vendor.exe";
myprocess.Start();
In your call to kill the process by name, you can first assure it will be found by not hard coding its name but instead using:
foreach (var process in Process.GetProcessesByName(myprocess.ProcessName))
{
process.Kill();
}
You can also use your process reference in a event that is fired on application close to combat your issue of it still running after the application has exited.
I have an application that launches another program and monitors it. When the program closes, my application also closes.
However, if I close my application first, the other program is still running.
ProcessStartInfo procInfo = new ProcessStartInfo("myProg.exe");
Process proc = new Process();
proc.StartInfo = procInfo;
proc.Start();
while (!proc.HasExited)
{
// do stuff
}
// On proc exit, my application is also done
How do I make sure that if I close my monitoring app, any processes that are being monitored are also killed?
So for example suppose MyApp is monitoring Notepad.
If I close MyApp, Notepad should also be closed.
Since this is a console application, capturing the "Exit" event is a bit trickier than forms. Take a look at this thread, I have it bookmarked because I was wondering the same thing as you a long time ago and it's handy to keep this around.
At that end of your program call:
if (!proc.HasExited)
proc.CloseMainWindow();
... to close the other process's window.
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.