How know when a winform is loaded by Process.Start?

早过忘川 提交于 2019-12-11 03:29:11

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/1177956/how-know-when-a-winform-is-loaded-by-process-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!