好了,這裏的交易....NET(C#)工藝與使用Adobe Acrobat
我在.NET 2.0(C#)在Windows(XP)程序,它允許用戶重命名一個給定的.pdf文件。 (文件名是「結構描述性的」,因爲它列出了關於文件內容的簡單信息。)在程序的唯一形式中,有一個LinkLabel對象,它允許用戶自己打開.pdf,以便他們可以看到他們在重命名。
訣竅是,當用戶進行適當的更改並單擊「保存」按鈕時,我需要顯示.pdf的Acrobat窗口關閉,執行保存,「下一個」文件爲檢索到,並立即打開一個新窗口打開顯示下一個文件。
下面是相關的代碼片段:在KillViewer(),有基本上鎖定抓循環,以確保該程序不嘗試重命名工作文件,直到PDF閱讀器後
private void OpenViewer()
{
// NOTE: pdfView is of type Process, in case you're not familiar with
// Process.Start().
pdfView = System.Diagnostics.Process.Start(lnkFile.Links[0].LinkData.ToString());
}
private bool KillViewer()
{
bool result = (pdfView != null);
if (pdfView != null)
{
pdfView.CloseMainWindow();
pdfView.Close();
pdfView.Dispose();
pdfView = null;
GC.Collect();
// Verify that the lock is available before you return, as returning basically says:
// "Yup, the file's available."
bool locked = false;
StreamWriter sw = null;
do
{
try
{
sw = new StreamWriter(new FileStream(lnkFile.Links[0].LinkData.ToString(), FileMode.Open));
locked = false;
}
catch (Exception)
{
locked = true;
}
} while (locked);
sw.Dispose();
}
return result;
}
private void SomeButtonEvent
{
// Record whether a viewer was open in the first place.
bool viewerActive = KillViewer();
PerformFileLockingMethod();
GetNextFile()
if(viewerActive)
{
OpenViewer();
}
}
公告已經完全釋放鎖定。
問題是這樣的:有時這一切都很精美,有時KillViewer在CloseMainWindow()調用中出現故障,並帶有InvalidOperationException,details =「進程已退出,因此請求的信息不可用。」。這將是相當簡單的,如果不是因爲兩件事情...
1:pdfView.HasExited =真
和
2:的該死的PDF閱讀器仍然是開放的!
這個世界怎麼可能這樣?是否有一個流程命令我應該用來確保窗口關閉?僅供參考,該程序在System。*命名空間或內部生成的類之外不引用任何其他內容,該類也最終僅引用System。*。
謝謝。
也許Acrobat中叉掉在某些情況下,單獨的瀏覽器進程。您是否檢查過您開始的進程的進程ID是否與仍處於打開狀態的查看器相匹配? – 2010-01-09 02:53:44