C#吊牌打印服务:Print()方法与打印队列相关技术咨询
Hi there! I’ve worked on several .NET-based printing services (including hangtag and label printing) so I can break this down clearly for you.
First, let’s tackle your core questions about your current code:
Does the print job get added to the print queue?
Absolutely yes. When you call pd.Print(), the .NET PrintDocument class interacts with the Windows Print Spooler service to submit your job. This job will show up in the standard Windows print queue (you can view it via Settings > Bluetooth & devices > Printers & scanners or the classic Control Panel). This happens whether you’re targeting a local or network-connected printer.
What happens when the printer is busy?
When the printer is already processing another job, your hangtag print job will wait in the print queue in the order it was submitted. Here’s a closer look at the flow:
- Your
Print()call will block only until the job is successfully handed off to the print spooler (not until the job finishes printing). Once submitted, your code will resume executing. - If the printer goes offline, runs out of paper, or hits an error mid-print, your job will stay in the queue with a "paused" or "error" status until the issue is fixed (or the job is canceled).
- In some cases, if the print spooler can’t reach the printer at all (e.g., network outage), calling
Print()may throw aPrinterException—so adding explicit error handling for this scenario is a smart move.
Best Practices for Queue Handling (Since You Mentioned Your Current Approach Isn’t Optimal)
For hangtag printing (especially batch scenarios), here are actionable improvements you can make:
Use asynchronous printing
Instead ofPrint(), usePrintAsync()(available in .NET Framework 4.5+ and .NET Core/.NET 5+). This avoids blocking your application’s main thread, which is critical if you’re printing multiple hangtags in a batch—your UI won’t freeze while jobs are being submitted. Example snippet:try { // ... set up PrintDocument as before pd.PrintAsync(); pd.PrintCompleted += (sender, e) => { // Handle completion or errors here if (e.Error != null) { // Log or notify users about printing failure } }; } catch (PrinterException ex) { // Handle printer connectivity/queue issues }Batch multiple hangtags into a single print job
Instead of creating a newPrintDocumentfor each hangtag, render all your hangtags within a singlePrintPage(or multiplePrintPageevents if they span pages). This reduces the number of jobs in the queue, making it easier to manage and cutting down on print spooler overhead.Monitor print job status
If you need to verify whether a hangtag job actually printed successfully, you can query the Windows print queue using WMI (Windows Management Instrumentation) or theSystem.Printingnamespace. This lets you check if a job completed, was canceled, or failed.Enhance error handling
Your current code only closes theStreamReaderin afinallyblock. Add acatchblock forPrinterExceptionandInvalidPrinterExceptionto handle cases where the printer is unavailable, the queue is full, or the printer name is incorrect. This helps you gracefully notify users or retry the job.Consider a dedicated print queue
If hangtag printing is a high-volume task, set up a dedicated print queue for your service. This isolates your jobs from other general-purpose print tasks, reducing the chance of your hangtags being delayed by large documents.
Your original code for reference:
streamToPrint = new StreamReader(@"C:\printerlog\rootlog2.log"); try { printFont = new Font("Arial", 10); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); // Print the document. pd.PrinterSettings.PrinterName = "HP LaserJet M1522 series PCL6 Class Driver"; pd.Print(); } finally { streamToPrint.Close(); }
内容的提问来源于stack exchange,提问作者João Silva

