[英]Starting process is not always starting
I have the following function: 我有以下功能:
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;
}
So the problem is that sometimes my application that I am trying to start with this function is not starting (It starts about 80% of the time). 因此,问题在于,有时我尝试使用此功能启动的应用程序无法启动(大约80%的时间启动)。 However I do go through the part of the code that checks to make sure it started and outputs as such.
但是,我确实检查了代码部分,以确保它已启动并按原样输出。 I am not sure why it is not starting.
我不确定为什么没有开始。 I double click the application when I see it doesnt start to make sure its a valid exe.
当我看到该应用程序没有开始以确保其有效的exe时,我双击该应用程序。 It always is and starts fine.
它始终是,并开始正常。 I have also tried using the shell and not using the shell.
我也尝试过使用外壳而不使用外壳。 No difference.
没有不同。
I am thinking that processToExecute is getting cleaned up before the application has successfully started all the way. 我认为processToExecute在应用程序一直成功启动之前会被清理。 Just a guess though.
只是一个猜测而已。
I appreciate your help in advance. 非常感谢您的帮助。
I put in a few sleeps to see if it was just happening too fast. 我睡了一会儿,看看它是否发生得太快了。
The reason 20% of time your application don't show up when started by Process because of the time the application takes load before we see the user Interface 原因是由于应用程序在我们看到用户界面之前需要加载的时间,因此在由Process启动时20%的时间未显示的原因
So their are two ways through which you might be able to achieve it 因此,它们是您可能能够实现的两种方法
1. start the process - > process.Start(); And then process.WaitForInputIdle();
OR 要么
2. start the process - > process.Start(); And then Thread.Sleep(1000);
//make sure you give reasonable milliseconds
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.