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

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.

Related

Pass cmd argument to running process when shutting down windows service

I am creating a windowservice that starts a process where I pass multiple cmd arguments in the StartInfo.Arguments. That works fine as expected.
The problem is that when i stop the windowsservice, I need to tell the running program to shut down gracefully before the windows service stops. Because if I just kill the process the program does not save the collected data. The program that I run is the Process Monitor and it accepts the following command "/terminate".
How can I pass the /terminate argument before my windows service stops?
There is a couple of ways you can approach this solution but you can signal the process, and based on what you send with this signal it should execute its 'clean-up' code. Once it's finished, the process that spawned it should be signalled that it finished the cleanup code and can then shutdown. This of course assumes that you wrote the code for both the Window service and the process being spawned.
You can try Named pipes (https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-use-named-pipes-for-network-interprocess-communication)
I managed to find a solution. In the OnStop() method in the service, I needed to create a new instance of the Process Monitor and pass the /Terminate argument:
Process process = new Process();
process.FileName = "\Procmon.exe" ;
process.StartInfo.Arguments = "/AcceptEula /Terminate";
process.Start();
With that it stoppes all the Process Monitor instances gracefully.

WinForm Application -> End Process -> FormClosing() & FormClosed() events Not Fired

I have a started win-form application with some codes in FormClosing() & FormClosed() events.
With another application i want to forcibly close that first application like this :
string procId = proc.Id.ToString();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "taskkill.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "/f /t /pid " + procId;
Process.Start(startInfo);
When i want for forcibly close first application by those codes or using with Task Manager -> End Process , FormClosing() & FormClosed() events are not fired.
How can i force those events to fire in every situation or what is the solution?
See Prevent C# app from process kill.
You cannot prevent your app's process being terminated, and you won't get an event it is being terminated.
So you must cater for the case where a process didn't clean up nicely at shutdown. For that matter, you need to do that anyway, what if the machine BSODs while your app is running? You can never assume the previous instance completed successfully.
It looks like your app closes some data file upon shutdown, so that the next time the program runs it can read the file again. If that is the case, you could write some "dirty flag" in or near the file and clean the file up on startup if it is flagged.
As explained in How does task manager kill my program? and MSDN: Terminating a Process (Windows):
Note that you cannot intercept or react upon TerminateProcess. Your process will die and there is nothing you can do before that happens.
If a process is terminated by TerminateProcess, all threads of the process are terminated immediately with no chance to run additional code. This means that the thread does not execute code in termination handler blocks.

See the window of a batch file that I start from IIS ASP.NET WebAPI thread

In my project I need to start a batch file from a thread on the server and wait for it to finish. My code is:
var process = new Process();
process.StartInfo.FileName = batchFilePath;
process.StartInfo.Arguments = "-0";
process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
process.WaitForExit();
and in the batch is simply put "pause".
The problem is that is don't see any window open and the thread is stuck and wait for the batch to finish.Only when I close the process by the windows task manager the program continues. The problem happens also if I put in the batch "cmd". How can I see the window? I want to make sure that the batch is running correctly.
This is a batch file running on the server? And you are not logged into the server, but into a web client?
You won't be able to see the console window from a web client, because it's running on a different machine.
If you are logged into the server, but still aren't seeing the window, you could try researching and experimenting with the ProcessStartInfo properties CreateNoWindow or UseShellExecute.
You might also want to look into RedirectStandardInput and RedirectStandardOutput if you need to see (or inspect programmatically) what the process writes to the console 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.

Categories

Resources