C # - Creating a process. Start until the process begins.

I need to make sure that the process is running before moving on to the method.

Statement:

Process.Start("popup.exe"); 

Can you execute the WAIT command or set a delay for this value?

+72
c # windows process
Jun 17 '11 at 18:18
source share
9 answers

Do you mean wait until this is over? Then use Process.WaitForExit :

 var process = new Process { StartInfo = new ProcessStartInfo { FileName = "popup.exe" } }; process.Start(); process.WaitForExit(); 

Alternatively, if this is a user interface application that you expect to enter in a message loop, you can say:

 process.Start(); process.WaitForInputIdle(); 

Finally, if none of them apply, just Thread.Sleep for some reasonable amount of time:

 process.Start(); Thread.Sleep(1000); // sleep for one second 
+127
Jun 17 '11 at 18:21
source share
โ€” -

I also needed this one, and I checked the title of the process window. If this is the one you expect, you can be sure that the application is running. The application I tested took some time to start, and this method worked fine for me.

 var process = Process.Start("popup.exe"); while(process.MainWindowTitle != "Title") { Thread.Sleep(10); } 
+15
Jun 17 '11 at 18:39
source share

The answer "ChrisG" is correct, but we need to update MainWindowTitle every time, and it is better to check for empty .... like this:

 var proc = Process.Start("popup.exe"); while (string.IsNullOrEmpty(proc.MainWindowTitle)) { System.Threading.Thread.Sleep(100); proc.Refresh(); } 
+12
Mar 04 '16 at 6:26
source share

First of all: I know this is pretty old, but no answer has been accepted so far, so maybe my approach will help someone else. :)

What I did to solve this problem:

 process.Start(); while (true) { try { var time = process.StartTime; break; } catch (Exception) {} } 

The association var time = process.StartTime will throw an exception until the process starts. Therefore, as soon as this passes, we can safely assume that the process is running and continue to work with it. I use this to wait for the Java process to start, as it takes some time. Thus, it should be independent of which machine the application is running on, and not using Thread.Sleep() .

I understand that this is not a very clean solution, but the only thing that should be independent of the performance that I could think of.

+8
Mar 23 '17 at 19:52
source share

As others have said, itโ€™s not immediately obvious what you are asking. I'm going to assume that you want to start the process, and then perform another action when the process is "ready."

Of course, โ€œreadyโ€ is a difficult bit. Depending on what you need, you may find that just waiting is enough. However, if you need a more robust solution, you can use the Mutex name to control the flow of control between the two processes.

For example, in your main process, you can create a named mutex and start a thread or task that will wait. Then you can start the second process. When this process decides that it is โ€œready,โ€ it can open the named mutex (you must, of course, use the same name) and pass the signal to the first process.

+7
Jun 17 2018-11-18T00:
source share

I agree with Tom. In addition, to check the processes when running Thread.Sleep, check the running processes. Something like:

 bool found = 0; while (!found) { foreach (Process clsProcess in Process.GetProcesses()) if (clsProcess.Name == Name) found = true; Thread.CurrentThread.Sleep(1000); } 
+4
Jun 17 '11 at 18:26
source share

Are you sure the Start method returns before the child process starts? It always seemed to me that Start synchronously starts a child process.

If you want to wait until your child process completes some initialization, you will need interprocess communication - see Interprocess communication for Windows in C # (.NET 2.0) .

+2
Jun 17 '11 at 18:24
source share

To expand on the idea of โ€‹โ€‹@ChrisG, consider using process.MainWindowHandle and see if the message box loop responds. Use p / invoke this Win32 api: SendMessageTimeout . By this link:

If the function succeeds, returning a value other than zero. SendMessageTimeout does not provide information about individual windows failing if HWND_BROADCAST is used.

If the function crashes or timed out, the return value is 0. To get extended error information, call GetLastError. If GetLastError returns ERROR_TIMEOUT, then the timed function of.

+1
Jun 17 '11 at 19:08
source share

I think the OP can refer to the need to have a valid window handle from the child process so that it can handle it somehow.

In my case, I want to create a Perl script to run in a DOS console window and center the window in my forms application when it starts. If I grab Process.MainWindowHandle too early and call โ€œMoveWindowโ€ on it, the handle is still invalid and the call does nothing. If I wait for the second (by calling Thread.Wait (1000)), the screen appears at its default location and suddenly moves after a second. Or it can be very unpleasant for the user.

By going into a loop and expecting Process.MainWindowTitle to return something meaningful, I can grab the window as soon as it responds and focus it on my form without annoying flicker.

The OP may be trying to do something like this.

+1
Dec 12 '12 at 13:40
source share



All Articles