How do I retrieve a list or number of jobs from a printer queue?

13,744

You can use the .NET 3.0 PrintQueue class in the System.Printing namespace. Its NumberOfJobs property tells you how many jobs are queued, GetPrintJobInfoCollection() returns details on all the jobs. Beware that it doesn't have any events that tells you that the job collection changed, you need to poll with a timer. Once a second or so ought to be fine.

Share:
13,744

Related videos on Youtube

Berry Ligtermoet
Author by

Berry Ligtermoet

BY DAY: I work as a web developer at FreshTag B.V. where we pursue our own ventures but also rent a helping hand to businesses in need for extra programming performance such as: Infostrada Sports, Patch Marketing and Justlease.nl. BY NIGHT: I play games or enjoy a nice evening of relaxing on the couch watching my favourite TV shows. I maintain the portfolio website for my sister who is an artistic and commercial photographer. FOR FUN: Pretty much what I do by night.

Updated on May 28, 2022

Comments

  • Berry Ligtermoet
    Berry Ligtermoet almost 2 years

    I'm looking for a way to get a list or number of jobs from a particular printer. In the best case I would want to have a "Job object" that represents one print job and its name in the print queue.

    This is required because I need to monitor the state of a printer so I can refill the print queue with a new batch of documents without overflowing the print spooler

    Thanks in advance!

    Edit: added code fragment of solution

    private int GetNumberOfPrintJobs()
    {
        LocalPrintServer server = new LocalPrintServer();
        PrintQueueCollection queueCollection = server.GetPrintQueues();
        PrintQueue printQueue = null;
    
        foreach (PrintQueue pq in queueCollection)
        {
            if (pq.FullName == PrinterName)
                printQueue = pq;
        }
    
        int numberOfJobs = 0;
        if (printQueue != null)
            numberOfJobs = printQueue.NumberOfJobs;
    
        return numberOfJobs;
    }
    
  • Berry Ligtermoet
    Berry Ligtermoet about 13 years
    I've had a look at this one already. But it requires a PrintServer object to instantiate. I'm using printers that are installed locally. Would this pose a problem? But this would certainly be an ideal solution. I already have a while() construction in place to poll as I'm printing in a thread and I need a blocking construction. Also this printer is very slow ;)
  • user1703401
    user1703401 about 13 years
    Use the LocalPrintServer class.
  • Berry Ligtermoet
    Berry Ligtermoet about 13 years
    Could it be that obvious? Thanks for showing me what Google couldn't come up with :)
  • Berry Ligtermoet
    Berry Ligtermoet about 13 years
    I asked a question following this one as I'm still stuck on how to target a specific machine using the LocalPrintServer: stackoverflow.com/questions/5645892/…
  • user1703401
    user1703401 about 13 years
    Note the word Local. That targets one very specific machine, the one that the program runs on.
  • Berry Ligtermoet
    Berry Ligtermoet about 13 years
    Ye sorry, I meant a specific printer. But I've already solved the matter with how to address the printspooler from a specific printer :) See the question for the code fragment.
  • binki
    binki over 5 years
    @BerryLigtermoet I figured that Server in PrintServer pretty much means the same thing as “daemon”. Just because a process running on your Windows client machine or even simply an API is called a “server” doesn’t mean that process is some other computer offering services, i.e., acting as a “server”.