A「神祕」的行爲在我的代碼的時候發現沒有異常。簡單地說,關鍵是神祕Win32Exception:製作一個斷點
- 與斷點工作線不不無斷點工作
- 它無關特權(不像很多關於'Win32Exception的提問做)
這裏報告了一個有點類似的症狀:MSBuild Script gets "Win32Exception: The system cannot find the File Specified" ...因爲這兩者都與調試過程有關。 (我不知道是否有事情做與我的問題,雖然)。
我寫信給
- 代碼生成文本格式
- 推出「gnuplot的」直方圖通過使用' System.Diagnostics.Process.Start「繪製生成的直方圖
- 還通過使用打開由gnuplot的生成的圖像文件」的Process.Start(‘generatedPlot.png’)。
1,2似乎成功執行,因爲我在工作目錄中得到一個PNG圖像。
然而,儘管劇情圖像的存在,我得到一個「Win32Exception」的說法「指定的文件未找到」當我嘗試打開該文件
void GeneratePlot()
{
// generate a png image called 'outputPath' with console 'gnuplot.exe'
MyClass.gnuplot(dataFilePath,outputPath);
MyClass.OpenFile(outputPath);
}
的OpenFile被簡單地定義爲
static void OpenFile(string fileToOpen)
{
Process.Start(fileToOpen); // this throws 'Win32Exception' ...(*)
}
的神祕的東西是在這裏:爲了調試這個問題,我把的斷裂點(*)。然後例外是不再拋出!
(注:由於劇情圖像成功創建,你不要在相同的「fileToOpen」第二次運行異常。因此,請確保您調試之前刪除生成的圖像陰謀。)
我設法找到比把一個斷點,當然其它的方式。我所做的只是分離MyClass.gnuplot和MyClass.OpenFile的執行:
void GeneratePlot()
{
// some code
MyClass.gnuplot(dataFilePath, outputPath);
}
void button1_Click(object sender, EventArgs e)
{
MyClass.OpenFile(outputPath);
}
後,我執行 'GeneratePlot()',點擊 'BUTTON1'。 這一次它顯示了PNG圖像!
以防萬一,我寫這樣的代碼來創建一個PNG圖像的情節:(它工作正常孤單!)
static void gnuplot(string dataFilePath, string outputPath)
{
Process p = new Process();
p.StartInfo.FileName = \\path\\to\\gnuplot.exe;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.WorkingDirectory = Directory.GetWorkingDirectory();
// some other StartInfo setting
p.Start();
// execute gnuplot with the following
StreamWriter sw = new StreamWriter(p.StandardInput);
sw.WriteLine("set terminal \"png\"");
sw.WriteLine("set output " + outputPath);
sw.WriteLine("plot '{0}'",dataFilePath);
}
我很好奇爲什麼發生這種情況。你能給我建議嗎?非常感謝你提前。
非常感謝! 'p.WaitForExit()'工作!並且我證實'Thread.Sleep(TimeSpan.FromSeconds(1.0)''工作。 只是爲了好玩,我也證實了我需要等待多少。原來'Thread.Sleep(TimeSpan.FromMilliseconds(200) '是門檻 - 等待199毫秒沒有工作! – user23763