私は次の機能を持っています:
public static void ExecuteNewProcess(string fileToExecute, Action<string> writeToConsole)
{
ProcessStartInfo startInfo = new ProcessStartInfo(fileToExecute);
Process processToExecute = new Process();
startInfo.UseShellExecute = true;
processToExecute.StartInfo = startInfo;
if (!File.Exists(fileToExecute))
{
throw new FileNotFoundException("File not found for execution");
}
if (processToExecute.Start())
{
Thread.Sleep(6000);
Process[] procs = Process.GetProcesses();
if (IsProcessOpen(Path.GetFileNameWithoutExtension(fileToExecute)))
{
writeToConsole(fileToExecute + " launched successfully...");
}
else
{
writeToConsole(fileToExecute + " started but not found.");
throw new Exception("Application started butnot found running...Delay = 6000, File Name = " + Path.GetFileNameWithoutExtension(fileToExecute));
}
}
else
{
writeToConsole("Error Launching application: " + fileToExecute);
throw new Exception("Application did not launch " + fileToExecute);
}
}
private static bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains(name))
{
return true;
}
}
return false;
}
したがって、問題は、この関数で起動しようとしているアプリケーションが起動しない場合があることです (約 80% の確率で起動します)。ただし、コードが開始され、そのように出力されることを確認するコードの部分を確認します。起動しない理由がわかりません。アプリケーションが有効なexeであることを確認するために起動しない場合は、アプリケーションをダブルクリックします。それはいつもうまくいきます。また、シェルを使用して、シェルを使用しないでみました。変わりはない。
アプリケーションが完全に正常に起動する前に、processToExecute がクリーンアップされていると考えています。推測ですが。
よろしくお願いします。
それがあまりにも速く起こっているかどうかを確認するために、私は数回寝ました。