[英]Reliability of Process.Start() then close current process by using C#
我有一個C#winform應用程序A,需要使用Process.Start啟動另一個winform應用程序B,然后關閉自身。 源代碼如下:
using(Process updater = new Process())
{
updater.StartInfo.FileName = "B.exe";
updater.Start();
}
this.Close();
但是我有時會注意到A關閉自身而不運行B。
我想知道如何在關閉A之前確保B.exe成功啟動。
順便說一句,我的目標框架應該是3.5&4。
提前致謝。
您可以檢查該過程是否已啟動並作出響應:
Boolean closeme = false;
using (System.Diagnostics.Process updater = new System.Diagnostics.Process())
{
updater.StartInfo.FileName = "B.exe";
if (updater.Start())
while (!updater.HasExited)
{
if (updater.Responding)
{
closeme = true;
break;
}
/* insert a timeout if process hangs */
}
if (updater.HasExited)
{
/* Do something else (updater.ExitCode) */
}
}
if (closeme)
this.Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.