2012-10-26 52 views
1

A「神祕」的行爲在我的代碼的時候發現沒有異常。簡單地說,關鍵是神祕Win32Exception:製作一個斷點

  • 與斷點工作線不無斷點工作
  • 它無關特權(不像很多關於'Win32Exception的提問做)

這裏報告了一個有點類似的症狀:MSBuild Script gets "Win32Exception: The system cannot find the File Specified" ...因爲這兩者都與調試過程有關。 (我不知道是否有事情做與我的問題,雖然)。

我寫信給

  1. 代碼生成文本格式
  2. 推出「gnuplot的」直方圖通過使用' System.Diagnostics.Process.Start「繪製生成的直方圖
  3. 還通過使用打開由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); 
} 

我很好奇爲什麼發生這種情況。你能給我建議嗎?非常感謝你提前。

回答

1

gnuplot創建和寫入文件的時間與嘗試在自己的進程中打開文件的時間可能存在競爭條件。我敢打賭,當你在調試器中運行它時,在gnuplot進程已經關閉輸出文件的時間已經過了足夠的時間。

要解決這個問題,可以在發送plot命令後等待一段時間,或者更好的是,等待gnuplot進程退出。

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); 

    // ----> wait for gnuplot to exit before returning 
    // (presumes that gnuplot exits shortly after executing the plot command) 
    p.WaitForExit(); 
} 

如果p.WaitForExit();聲明不起作用(即gnuplot的過程不執行plot命令後不會退出),嘗試Thread.Sleep(TimeSpan.FromSeconds(1.0));(或一些其他時間段)來代替。

+0

非常感謝! 'p.WaitForExit()'工作!並且我證實'Thread.Sleep(TimeSpan.FromSeconds(1.0)''工作。 只是爲了好玩,我也證實了我需要等待多少。原來'Thread.Sleep(TimeSpan.FromMilliseconds(200) '是門檻 - 等待199毫秒沒有工作! – user23763

1

我同意門羅,這是由於競爭條件。此外,他建議什麼,我建議改變你的OpenFile方法:

if (System.IO.File.Exists(fileToOpen)) 
{ 
    Process.Start(fileToOpen); 
} 
else 
{ 
    // handle missing file scenario 
} 

這樣一來,你就還可以,如果因爲任何其他原因不生成圖像文件覆蓋。

+0

非常感謝你!你的代碼也起作用了。也就是說,如果我在調用gnuplot進程後直接調用'Process.Start(fileToOpen)',就像我發佈的那樣''File .exists(fileToOpen)'返回'false'。 (我的'聲望'不夠高,不能投票,對不起..) – user23763