2017-09-15 102 views
1

我需要用ProcessStartInfo打印pdf文件。ProcessStartInfo WaitForExit超時

string docInvoicePath = @"[Path]"; 
string printername = "\"PRN-OFFICE\""; 
string driver = "\"Xerox Global Print Driver PS\""; 
string port = "\"[IP]\""; 

ProcessStartInfo psInfo = new ProcessStartInfo 
{ 
    FileName = @"""C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32""", 
    Arguments = String.Format("/s /o /h /t " + docInvoicePath + " " + printername + " " + driver + " " + port), 
    Verb = "print", 
    WindowStyle = ProcessWindowStyle.Hidden, 
    CreateNoWindow = true, 
    UseShellExecute = false     
}; 

Process process = Process.Start(psInfo); 
process.WaitForExit(6000); 
if (process.HasExited == false) 
{ 
    process.Kill(); 
} 
process.Close(); 

filenamearguments是正確的cmd粘貼時,他們的工作。 該代碼可以正常工作,但在Process.Start之後,當涉及到WaitForExit時,程序沒有完成。我得到超時錯誤:

System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.WaitHandle.WaitOneNative(SafeHandle waitableSafeHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext) at System.Threading.WaitHandle.InternalWaitOne(SafeHandle waitableSafeHandle, Int64 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext) at System.Diagnostics.Process.WaitForExit(Int32 milliseconds)...

我搜索並嘗試了一些東西,如設置<httpRuntime executionTimeout="300"/>並在代碼process.WaitForExit(6000);和更高療法那裏得到了印沒有exeption但沒有碰上或類似。

有沒有錯誤或我錯過了什麼?

編輯: 我改變了我的代碼塊上面。現在代碼在調試模式下工作,但它仍然不會在發佈時打印我的文檔。我也嘗試使用不同的用戶。 在調試模式下,代碼打印文檔,但運行在kill查詢。

ProcessStartInfo.Verbs返回一個參數異常但我不知道爲什麼。

+0

你是否阻止主線程? – Fildor

+0

我不這麼認爲,我該如何檢查? – MaxW

+0

我只是想知道爲什麼它應該被中止,如果在ASP.NET中,我不知道如果阻塞主線程這麼久會發生什麼。在普通的.net/WinForms應用程序中,您的GUI將無響應。但在ASP上 - 我真的不知道是否有機制中止長時間運行的東西。出於某種原因你是否必須等待退出?如果不是隻是省略它。如果是,請嘗試在工作線程上執行所有這些操作,並查看是否也有中止。 – Fildor

回答

0

經過大量的嘗試,測試和搜索,我相信我的代碼有效。 所以我仍然不知道爲什麼我的代碼停止工作。 但是從服務器上的Adobe Reader 9.0更改爲7.0,現在可以使用。

當我用Adobe Reader 9.0在本地進行調試時,它也起作用了,所以我想也許在網絡服務器上有更新。我還沒有驗證過。