C# Process.Start()调用后的代码未运行,但在调试C时工作#
下面是调用C# Process.Start()调用后的代码未运行,但在调试C时工作#,c#,process,C#,Process,下面是调用Process.Start()的函数 我只是在WinMerge中打开两个文件夹。此部件工作正常,并弹出此应用程序。但在此之后编写的代码都不会运行。但是,当我在p.Dispose()或p.Start()上放置一个断点并按continue时,之后的一切都正常工作 private void openWinMerge(string leftFile, string rightFile) { string args = "/C /f *.xml
Process.Start()
的函数
我只是在WinMerge中打开两个文件夹。此部件工作正常,并弹出此应用程序。但在此之后编写的代码都不会运行。但是,当我在p.Dispose()
或p.Start()
上放置一个断点并按continue时,之后的一切都正常工作
private void openWinMerge(string leftFile, string rightFile)
{
string args = "/C /f *.xml " + leftFile + " " + rightFile;
Process p = new Process();
p.StartInfo.FileName = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe";
p.StartInfo.Arguments = args;
p.StartInfo.CreateNoWindow = true;
if (p.Start())
{
p.Dispose();
return;
}
}
在这里,我调用调用函数调用Process.Start()
。下面的代码openWinMerge()
都不会运行
private void btnStart_Click(object sender, EventArgs e)
{
if (txtSerial.Text.Length == 9)
{
PO.Number = txtSerial.Text.ToUpper();
PO.SerialSearch = true;
if (PO.Search())
{
txtPO.Text = PO.Field.ProductionOrderNumber;
search();
createFolders();
copyDefaultFiles();
copyBackupFiles();
openWinMerge("\"" + Path.Combine(path, "INITIAL") + "\"", "\"" + Path.Combine(path, "Default") + "\"");
copyFinalXML();
return;
}
MessageBox.Show("Invalid Serial");
} else if (txtPO.Text.Length == 12)
{
PO.Number = txtPO.Text.ToUpper();
if (PO.Search())
{
txtSerial.Text = PO.Field.SerialNumber;
search();
createFolders();
copyDefaultFiles();
copyBackupFiles();
openWinMerge("\"" + Path.Combine(path, "INITIAL") + "\"", "\"" + Path.Combine(path, "Default") + "\"");
copyFinalXML();
return;
}
MessageBox.Show("Invalid PO or Missing Serial");
} else
{
MessageBox.Show("Please Enter either Serial or PO of Analyzer");
}
}
更新:
我在下面写了一些其他代码,它运行了,所以我的问题在copyFinalXML()
这是代码
private void copyFinalXML()
{
try
{
string[] files = Directory.GetFiles(Path.Combine(path,"INITIAL"));
Task.Delay(200);
foreach (string file in files)
{
// Will not overwrite if the destination file already exists.
string[] folders = file.Split('\\');
string filename = folders[folders.Length - 1];
if (filename.Equals("config.xml") || filename.Equals("service.xml") || filename.Equals("system.xml"))
{
string d = Path.Combine(Path.Combine(path, "FINAL"), filename);
File.Copy(file, d);
Task.Delay(500);
openNotePad(d);
}
}
}
// Catch exception if the file was already copied.
catch (IOException copyError)
{
MessageBox.Show(copyError.Message);
}
}
private void openNotePad(string filename)
{
//Process.Start("C:\\Program Files(x86)\\Notepad++\\notepad++.exe", "\"" + filename + "\"");
Process.Start(@"notepad++.exe", "\"" + filename + "\"");
return;
}
添加
p.WaitForExit()
在您的代码运行完流程后要执行的代码之前添加到您的代码。什么是CreateNoWindow=true
呢?为什么要处理新启动的流程?您是否希望进程已退出。。。因为它可能还没有退出。您应该调用p.WaitForExit()代码>并使用using语句,而不是手动处理。您是否可以提供一个命令行,可以粘贴到控制台,并且WinMerge确实可以正确打开-因为在中,我看不到任何/C参数@mjwills。我们当中可能有一个人误解了。可能是我!:)(问题不是很清楚)根据我收集的信息,winmerge将一个文件输出到磁盘。copyFinalXML可能会在winmerge完成写入之前尝试读取该文件。在调试模式下,单步执行可能会使进程在瞬间完成工作。因此,在继续之前等待winmerge完成似乎是必要的。不管是否处理它都无关紧要,但这表明普雷斯顿可能认为该过程已经完成,而事实并非如此。@BitLauncher我指的是WaitForExit
方法参见