How can I get the status of the printer in a timely manner in C# Windows Form App

Pat Hanks 40 Reputation points
2024-02-05T21:40:39.52+00:00

I am working on a C# Windows Form app and I need to print a sheet of labels on a laser printer. I created a printer dialog that allows the user to select which label is the starting position and a status field that gets updated once the user selects the printer from the combobox. I use a timer to check the status every 5 seconds and update the form field. I tried using System.Drawing.Printing.PrinterSettings.IsValid to get the printer status, but it always returns true. I also attempted to use System.Printing.LocalPrintServer, but I don't see the namespace listed. I then tried using WMI by installing Windows.Compatibility to get the System.Management namespace, but WMI can take a couple of minutes to see a state change. The Windows Print Queue manager will see the state change in seconds. How can I install System.Printing namespace and using LocalPrintServer to get the printer status promptly? I am using VS v17.8.6 and the target framework is .NET 7.0.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,802 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,969 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 18,281 Reputation points Microsoft Vendor
    2024-02-06T02:41:54.84+00:00

    Hi @Pat Hanks , Welcome to Microsoft Q&A,

    For the more convenient and easy-to-use methods, I recommend you to use the .Net Framework platform, which is more stable for Windows development.

    Generally, you can use PrintQueue in System.Printing to query the status you need.

    However, if your printer is not the default printer, you will need to load that printer's queue instead. What you need to do is not call, you need to load the correct queue by calling.

    using System.Printing;
    
    class Program
    {
         static void Main()
         {
             LocalPrintServer server = new LocalPrintServer();
    
             PrintQueue queue = server.DefaultPrintQueue;
    
             // Get printer properties
             var isOffline = queue.IsOffline;
             var isPaperJammed = queue.IsPaperJammed;
             var needUserIntervention = queue.NeedUserIntervention;
             var hasPaperProblem = queue.HasPaperProblem;
             var isBusy = queue.IsBusy;
    
             // Some example checks for printer status
             if(isOffline)
             {
                 Console.WriteLine("Printer is offline.");
             }
    
             if (isPaperJammed)
             {
                 Console.WriteLine("Paper jam detected.");
             }
    
             if (needUserIntervention)
             {
                 Console.WriteLine("User intervention is required.");
             }
    
             if(hasPaperProblem)
             {
                 Console.WriteLine("Paper problem detected.");
             }
    
             if(isBusy)
             {
                 Console.WriteLine("Printer is busy.");
             }
         }
    }
    

    A more complex method is to use com to poll the target device you are connected to and query its status.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful