2

I need to print a pdf-file with ProcessStartInfo.

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

The filename and arguments are correct they work when pasted in cmd. The code works properly but after Process.Start when it comes to WaitForExit the programm doesn`t finish. I get the timeout error:

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

I searched and try a few things like setting <httpRuntime executionTimeout="300"/> and run into a 500 or like in the code with process.WaitForExit(6000); and higher ther where no exeption but nothing got printed.

Is there an error or am i missing something?

EDIT: I changed my codeblock above. Now the code works in debug mode but it still won´t print my document when published. I also tried to use a different User. In debug mode the code prints the document but runs in the kill query.

The ProcessStartInfo.Verbs returns a Argument Exception but i don´t know why.

MaxW
  • 421
  • 1
  • 7
  • 22
  • Are you blocking the main thread? – Fildor Sep 15 '17 at 11:20
  • i don`t think so, how can i check this ? – MaxW Sep 15 '17 at 11:22
  • I was just wondering why it should be aborted and if in ASP.NET I am not sure what happens if you block the main thread for so long. In an ordinary .net/WinForms app your GUI would be unresponsive. But on ASP - I do not really know if there are mechanisms to abort long running stuff. Do you have to wait for exit for some reason? If not just omit it. If yes, try doing all this on a worker thread and see if the abort happens there, too. – Fildor Sep 15 '17 at 11:25
  • without WaitForExit it perfectly runs the code but nothing happens / gets printed – MaxW Sep 15 '17 at 11:27
  • Hmmm, sorry. I am really not familiar with ASP. Any suggestion from my side would only be applicable to "ordinary" .net Applications. I *suspect* you have to spawn a background task, though. – Fildor Sep 15 '17 at 11:32
  • There may be more to be done ... See this question: https://stackoverflow.com/q/8061362/982149 – Fildor Sep 15 '17 at 11:35
  • try with `UseShellExecute = false;` – It's a trap Sep 15 '17 at 11:47
  • `UseShellExecute = false;` didn´t work – MaxW Sep 18 '17 at 06:34

1 Answers1

0

After a lot of trying, testing and searching i am sure my code works. So i still don´t really know why my code stoped working. But changing from Adobe Reader 9.0 on the server to 7.0 it now works.

When i was debuging locally with Adobe Reader 9.0 it also worked, so i think maybe there was an update on the webserver. I diden`t verifyed that yet.

MaxW
  • 421
  • 1
  • 7
  • 22