進程 WaitForExit 不等待 (Process WaitForExit not waiting)


問題描述

進程 WaitForExit 不等待 (Process WaitForExit not waiting)

我創建了 Print spooler 應用程序來異步打印 pdf。

(應用程序使用veryPDF 命令從網絡打印機打印)

這裡是代碼

   var procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", " /c" + "E:\pdfprint_cmd\pdfprint.exe ‑$ 388444444448350FA394 E:\PrintSpoolerApplication\PrintSpoolerApplication\bin\Debug\45940.pdf");
   procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
   procStartInfo.Verb = "runas";
   procStartInfo.UseShellExecute = false;
   procStartInfo.CreateNoWindow = true;
   var proc = new System.Diagnostics.Process();
   proc.StartInfo = procStartInfo;
   proc.Start();
   proc.WaitForExit();

// Some stuff

但它沒有等待 WaitForExit 代碼。即使我的文檔在打印機隊列中,它也確實執行了(這裡//有些東西)。

還有其他方法可以在打印完成時通知嗎?


參考解法

方法 1:

Your code waits for cmd.exe to finish, which (probably) terminates immediately after it has started pdfprint.exe as a child process. I suggest you

  • either start pdfprint.exe directly (why do you need the Windows command line here anyway?)
  • or find the Process object of the child process ‑‑ e.g. through WMI, as described here ‑‑ and wait for that process to exit instead.

However, both approaches only work if pdfprint.exe actually waits for the scheduled print job to be completed. I don't know the tool, so I have no idea if it behaves that way. If it doesn't, you would have to access the print queue, which (as pointed out by Hans in his comment) is not recommended.

方法 2:

Here's a If‑Everything‑Else‑Fails approach you might have to take (if pdfprint.exe doesn't wait until completion for termination). It's ugly, but it'll work:

  1. Enter a while loop until the destination file exists (ideally with a timeout, in case pdfprint.exe runs into a problem and never generates the file)
  2. Enter a second while loop, where every X milliseconds (say, 500? 1000?), it checks the file length of the output PDF. If the file size is the same between checks, you assume the output process is finished and begin working with the file.

Like I said, it's not pretty (and you shouldn't use it unless you have no recourse)... but it gets the job done. I've had to use this sort of approach for handling incoming files being copied over the network (you need to process files when they come in, but you need to know that the file is finished being copied before touching it, because PDFs will lock as soon as they're opened and then the file‑copy will fail.)

方法 3:

Have you tried this?

procStartInfo.LoadUserProfile = true;

(by MunavvarRobert PetermeierKevinNotHere)

參考文件

  1. Process WaitForExit not waiting (CC BY‑SA 2.5/3.0/4.0)

#printing #.net #process #processstartinfo #C#






相關問題

是否可以創建網頁打印視圖的 PDF? (Is it possible to create a PDF of a webpage's print view?)

使用 PHP 打印到共享的 windows 打印機 (Linux PHP Server) (Using PHP to print to a shared windows printer (Linux PHP Server))

printf 和自定義類 (printf and custom class)

Firefox 無法訪問同一域上的 iframe 打印 (Firefox can't access iframe print on the same domain)

在python 3中動態打印一行 (Printing a line dynamically in python 3)

用於打印 div 的 JS 函數 - 在 Chrome 中不起作用? (JS function to print div - Doesn't work in Chrome?)

進程 WaitForExit 不等待 (Process WaitForExit not waiting)

你能給我打印屏幕並在javascript或flash中轉換為jpg的功能嗎 (Could you please give me the function of taking print screen and convert in to jpg in javascript or flash)

當我使用轉換時,我的打印輸出看起來不像打印預覽 (My printout doesn't look like the print preview when I use a Transform)

如何在 C# 中使用 PrintDialog 打印文檔 (How to print a document using PrintDialog in C#)

MKMapView 遮擋是否剔除它的註釋? (Does MKMapView occlusion cull it's annotations?)

在網絡環境中從 Brother TD-4100N 打印機檢索打印機狀態 (Retrieving the printer status from the Brother TD-4100N printer in a network environment)







留言討論