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.
Related
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 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.
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.
I am getting an exception on my server side code, which is serving up a silverlight app,
Win32Exception - No such interface supported
Our server side C# code starts up a separate process for a short task because of a third party dll not being thread safe. So the error above occurs in part of the code like this,
Process process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.CreateNoWindow = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.FileName =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "PreviewGenerator.exe");
process.StartInfo = processStartInfo;
process.Start(); // THIS IS WHERE THE EXCEPTION OCCURS
process.WaitForExit();
The PreviewGenerator.exe process does not start when it is not working, the exception occurs where the comment is above.
UPDATE:
I have run process monitor on the IIS server when the issue occurs. This shows that the w3wp process does this,
Thread Create
Access the file PreviewGenerator.exe
Hive unloaded (this is the registry)
Thread Exit
And it does this before calling the other process. If I compare this with a the process monitor log when it is working it does this,
Thread Create
Access the file PreviewGenerator.exe
Process Start
Does heaps of stuff with PreviewGenerator.exe including reading / writing / registry, etc.
Process Exit
Hive unloaded
Thread Exit
But process monitor does not show any information as to why the first case doesn't work.
Is there a way I can see why the thread exits prematurely?
Also I think this problem relates to when my server is being loaded up more, and much more memory is being used. How can I prove this?
I had a similar issue, I used
processStartInfo.UseShellExecute = false;
and that fixed it for me.
http://www.progtown.com/topic31343-process-start-processstartinfo-startinfo.html
I found the best thing to do was to create a separate app pool for my application in IIS and set an upper limit for the amount of RAM it could use. Also I found it useful to turn on the 'Generate Recycle Event Log Entry' items under the app pool settings.
You can then go to the system event log and filter out the items with a source of 'WAS' to understand what is going on in the app pools, when they are restarting and when they stop from being idle etc.
I think the main problem in our case is that the IIS box was running out of memory. Tuning the app pools and adding some extra RAM seems to have solved it.
I am starting a VLC job to record some streaming audio from within a c# function as follows (the actual args aren't relevant to the question):
Process proc = new Process();
proc.StartInfo.FileName = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
proc.StartInfo.Arguments = "someArgs";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
If all works correctly, the Start() function returns immediately and a process is started locally (i.e. VLC).
How can I get the process ID of this VLC job so I can kill it later? Using proc.Close() closes the process, but does not terminate the VLC job.
What is the most effective way to kill the VLC job? I have admin privilege locally.
How can I test that the job did start correctly? Is there some status flag on the proc object I can test?
Thanks
Andrew
The ID should be in your process object as proc.Id.
You can kill it with proc.Kill().
If there is a problem launching the process it will throw an exception (most likely a Win32Exception or an InvalidOperationException; see the help.)