仅在成功打印完成后才有办法检索Win32_Printjob信息吗?

时间:2019-05-01 06:59:00

标签: c# wmi

当前,我在用户打印但所有信息都存在时捕获了该事件,但是TotalPages不准确。该文档将是28页,但由于打印作业仍在处理中,因此仅显示10页。

我尝试过在while循环中循环,但这每次都返回相同的结果。尝试获取“从Win32_PrintJob中选择*,其中JobId = N”

private void StartWatcher()
        {
            try
            {
                txtBox.Text += "\r\nMonitoring for printer activity...";
                string wqlQuery = @"Select * From __InstanceCreationEvent Within 1
                    Where TargetInstance ISA 'Win32_PrintJob'";

                Classes.Printer.clsPrinter clsPrinter = new Classes.Printer.clsPrinter();
                WqlEventQuery query = new WqlEventQuery(wqlQuery);
                ManagementEventWatcher watcher = new ManagementEventWatcher(query);
                watcher.EventArrived += new EventArrivedEventHandler(printJobArrived_EventArrived);
                watcher.Start();
            }
            catch (Exception ex)
            {
                log.Error("Form1 StartWatcher: " + ex.Message.ToString());
                MessageBox.Show(ex.ToString());
            }
        }

void printJobArrived_EventArrived(object sender, EventArrivedEventArgs e)
        {
            try
            {

                string note = "";


                ManagementBaseObject printJob = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
                note = "";
                note = "\r\n\r\nDescription :" + printJob.GetPropertyValue("Description").ToString();
                note += "\r\nTotal Pages:" + printJob.GetPropertyValue("TotalPages").ToString();
                //GetDetails(Convert.ToInt32(printJob.GetPropertyValue("JobId")));
                Upload(printJob.GetPropertyValue("TotalPages").ToString(), printJob.GetPropertyValue("Description").ToString());

                //foreach (PropertyData prop in printJob.Properties)
                //{
                //    string val = prop.Value == null ? "null" : prop.Value.ToString();
                //    note += "\r\n" + "\r\n" + prop.Name + ": " + val;
                //}

                txtBox.Invoke((Action)delegate
                {
                    txtBox.Text += note;
                });
            }
            catch (Exception ex)
            {
                log.Error("Form1 printJobArrived_EventArrived: " + ex.Message.ToString());
                MessageBox.Show(ex.ToString());
            }
        }

预期结果是它应该返回准确的Totalpages数,但是返回不正确的值。经过测试,打印为pdf

1 个答案:

答案 0 :(得分:0)

因此,在搜索之后,我找到了答案。主要问题是从

更改事件时触发的查询
string wqlQuery = @"Select * From __InstanceCreationEvent Within 1
                Where TargetInstance ISA 'Win32_PrintJob'";

string wqlQuery = @"Select * From __InstanceModificationEvent  Within 0.1
                Where TargetInstance ISA 'Win32_PrintJob' And TargetInstance.JobStatus = 'Printing'";