如何通过Printername和JobID暂停Win32_printJob

时间:2016-09-03 06:32:50

标签: c# .net winapi print-spooler-api printqueue

TASK

  

当用户打印文档时,暂停弹出将出现然后填写表单单击输入表单将关闭并且作业已恢复。

     

我们有 50台网络打印机 2000客户端计算机一台打印服务器

     

每个客户都有 3或4台打印机

问题

  

如果用户使用网络打印机(CANON,HP)在本地打印文档(EX:PDF打印机,XPS文档编写者)(或),打印作业立即生效PAUSE。

MY TRIES

当打印任何文档时,事件监听器正在观看并返回打印作业

  • 第一个暂停方法中,有时会工作,而且大部分时间都无法正常工作。因为它正在搜索printjob,但它已经打印过了。

    < / LI>
  • 第二个暂停方法不起作用因为,事件侦听器返回Managementbaseobject但是如果要暂停打印作业需要ManagementObject 如何将ManageBaseObject转换为ManageObject < /强>

  

PRINTJOB EVENT LISTENER CODE

    managementEvent = new ManagementEventWatcher();
    managementEvent.Query = new EventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 0.1 WHERE TargetInstance ISA 'Win32_PrintJob'");
    managementEvent.Scope = new ManagementScope(@"\root\cimv2");
    managementEvent.EventArrived += new EventArrivedEventHandler(printJobArrived_EventArrived);
    managementEvent.Start();
  

主要行动守则

private void printJobArrived_EventArrived(object sender, EventArrivedEventArgs e)
{
  try
     {
CurrentprintJob = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
    }
    catch(Exception ex){

                       }
}
  

暂停PRINTJOB方法1

 public bool PausePrintJob(string jobname)
            {
                bool isActionPerformed = false;
                try
                { 
                    string searchQuery = "SELECT * FROM Win32_PrintJob WHERE Name LIKE '%"+jobname+"%'";

                    ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(searchQuery);
                    ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
                    foreach (ManagementObject prntJob in prntJobCollection)
                    {
                                prntJob.InvokeMethod("Pause", null);
                                isActionPerformed = true;
                   }
                }
                catch (Exception ex)
                {
                    new LogFile().WriteErrorLog(ex.StackTrace, ex.Message);
                }
                return isActionPerformed;
            }
  

暂停PRINTJOB方法2

 public bool PausePrintJob(ManagementObject currentPrintJob, bool makePause)
        {
            bool isActionPerformed = false;
            try
            {
                {
                    if (makePause == true && currentPrintJob != null)
                    {
                        currentPrintJob.InvokeMethod("Pause", null);
                        isActionPerformed = true;
                    }
                    else if (makePause == true && currentPrintJob != null)
                    {
                        currentPrintJob.InvokeMethod("Resume", null);
                        isActionPerformed = false;
                    }

                }
            }
            catch (Exception ex)
            {
                new LogFile().WriteErrorLog(ex.StackTrace, ex.Message);

            }
            return isActionPerformed;
        } 

2 个答案:

答案 0 :(得分:2)

WMI太慢了。您可能需要使用FindFirstPrinterChangeNotificationFindNextPrinterChangeNotification

您可能会发现these examples useful

答案 1 :(得分:-1)

最后,我在MSDN官方文档网站上找到了用于控制打印机的解决方案,这是链接: https://www.codeproject.com/Articles/6592/A-Simple-Approach-for-Controlling-Print-Jobs-using

它显示了如何PauseResumeDelete打印作业。核心类是Win32_PrintJobprintJob的变量来自ManagementObjectSearcher对象,该对象搜索一个打印机实例。

printJob.InvokeMethod("Pause", null);
printJob.InvokeMethod("Resume", null);
printJob.Delete()

我仍然发现它的Python实现代码: http://timgolden.me.uk/python/wmi/cookbook.html#watch-for-new-print-jobs

import wmi
c = wmi.WMI ()

print_job_watcher = c.Win32_PrintJob.watch_for (
  notification_type="Creation",
  delay_secs=1
)

while 1:
  pj = print_job_watcher ()
  print "User %s has submitted %d pages to printer %s" % \
    (pj.Owner, pj.TotalPages, pj.Name)