Real number of TotalPages of a PrintJob (Win32_PrintJob)

一个人想着一个人 提交于 2019-12-01 10:47:42

问题


I'm querying to Win32_PrintJob WMI class every time there is a change with ManagementEventWatcher, I obtained data about it, such as: Document, HostPrintQueue, JobId, JobStatus, TotalPages, etc. But TotalPages is not representing the real number of page to print, Seems at the moment to obtain these data still the printjob doesn't finished to process and devolving a number of pages to print in that moment but the real total is other number, How to get the real number of a print job when finished it to process? Here is my code:

 ManagementEventWatcher createPrintJobWatcher;
        String strComputerName = ".";
        // Create event query to be notified within 1 milli second of a change in a service
        WqlEventQuery createPrintJobQuery = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 0.001 WHERE TargetInstance ISA \"Win32_PrintJob\"");

        createPrintJobWatcher = new ManagementEventWatcher();
        createPrintJobWatcher.Scope = new ManagementScope("\\\\" + strComputerName + "\\root\\CIMV2");
        createPrintJobWatcher.Query = createPrintJobQuery;
        // times out watcher.WaitForNextEvent in 1 seconds
        createPrintJobWatcher.Options.Timeout = new TimeSpan(0, 0, 1);
        //set the print event handler
        createPrintJobWatcher.EventArrived += new EventArrivedEventHandler(createPrintJobListener);

        createPrintJobWatcher.Start();

        Console.WriteLine("Listening...");

        Console.ReadLine();

createPrintJobListener method:

        static void createPrintJobListener(object sender, EventArrivedEventArgs e)
    {

        SelectQuery query = new SelectQuery("Win32_PrintJob");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        using (ManagementObjectCollection printJobs = searcher.Get())
            foreach (ManagementObject printJob in printJobs)
            {
                Console.WriteLine("c1:", printJob);
                Console.WriteLine("ID: {0}", printJob.GetPropertyValue("JobId").ToString());
                Console.WriteLine("name: {0}", printJob.GetPropertyValue("name").ToString());
                Console.WriteLine("status: {0}", printJob.GetPropertyValue("status").ToString());
                if (printJob.GetPropertyValue("JobStatus") != null)
                {
                    Console.WriteLine("JobStatus: {0}", printJob.GetPropertyValue("JobStatus").ToString());
                }
                else
                {
                    Console.WriteLine("JobStatus: NULLLLLL");
                }

                Console.WriteLine("PC: {0}", printJob.GetPropertyValue("HostPrintQueue").ToString());
                Console.WriteLine("TOTOAL PAGES: {0}", printJob.GetPropertyValue("TotalPages").ToString());                    
            }
    }

回答1:


WMI is probably not sufficient to do this.

Windows doesn't reliably provide the page count (or copies etc), so the only way to get accurate info is to pause the job and parse it. This is a non-trivial task, but here's a little more info.



来源:https://stackoverflow.com/questions/46533312/real-number-of-totalpages-of-a-printjob-win32-printjob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!