How know when a winform is loaded by Process.Start?
Question
I'm in a WindowsForm (c# .net 3.5) and on click of a button launch another external application (also .net 3.5) using Process.Start() and understand when it is available after i have launched it.
ProcessStartInfo psInfo = new ProcessStartInfo(@"MyApplication.exe");
psInfo.RedirectStandardOutput = true;
psInfo.RedirectStandardError = true;
psInfo.UseShellExecute = false;
psInfo.CreateNoWindow = true;
Process proc = Process.Start(psInfo);
proc... IsFullyLoaded()?
How can i do it?
Solution
To wait for the process to create its form, call the WaitForInputIdle
method.
To find out whether it's ready, try this:
bool isReady = proc.WaitForInputIdle(0);
Or, alternatively,
bool isReady = (proc.MainWindowHandle != IntPtr.Zero);
You can also use the MainWindowHandle property to send messages to the form using the SendMessage
API function
OTHER TIPS
You could pass an argument to the process letting it know how it was launched.
psInfo.Arguments = "-startedByProcess";
Then have the child process callback to the owner process to let it know it has started, via remoting or something like that.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow