我想在我的項目中使用iTextSharp
dll打印Windows應用程序中的pdf ..通過c#.net代碼打印pdf時的線程問題?
但是,我迄今使用的方法似乎並不可靠。 (有時它工作,有時它不)
我在一個Process類中組合整個過程並編寫以下代碼。
printProcess = new Process[noOfCopies];
// Print number of copies specified in configuration file
for (int counter = 0; counter < noOfCopies; counter++)
{
printProcess[counter] = new Process();
// Set the process information
printProcess[counter].StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "Print",
FileName = pdfFilePath,
ErrorDialog = false,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
};
// Start the printing process and wait for exit for 7 seconds
printProcess[counter].Start();
// printProcess[counter].WaitForInputIdle(waitTimeForPrintOut);
printProcess[counter].WaitForExit(waitTimeForPrintOut); //<--**This is line for discussion**
if (!printProcess[counter].HasExited)
{
printProcess[counter].Kill();
}
}
// Delete the file before showing any message for security reason
File.Delete(pdfFilePath);
的問題是,如果沒有PDF被打開,則過程正常工作......(它打印好),但如果任何PDF是開放的,然後WaitForExit(..)
方法就是不等待進程退出..因此該過程運行得太快,因爲我在打印後刪除文件,如果打印報告的計數器(次數..)超過一次,會給出錯誤..
我也使用Timer
來緩慢這個過程,但它不起作用。我不知道爲什麼。 也使用睡眠命令..但它使主線程去睡覺,因此也不適合我。
請建議我一些非常可靠的方法,這樣做.. :)
什麼應用是聯想與打印命令?我問,因爲我們在使用Adobe Reader時遇到類似問題,並轉移到福昕閱讀器。然後我們使用它的命令行而不是動詞來開始一個進程。似乎工作,我相信它榮幸WaitForExit。 –