1

任务

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

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

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

问题

如果用户在本地打印文档(例如:PDF PRINTER、XPS DOCUMENT WRITER)(或)使用网络打印机(CANON、HP),则打印作业立即暂停。

我的尝试

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

  • 第一次暂停方法中有时会起作用,并且大部分时间都无法正常工作。因为,它正在搜索打印作业但它不存在已经打印。

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

打印作业事件侦听器代码

    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){

                       }
}

暂停打印作业方法 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;
            }

暂停打印作业方法 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;
        } 
4

2 回答 2

3

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

您可能会发现这些 示例 很有用

于 2016-09-09T12:13:10.940 回答
-1

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

它显示了如何PauseResume以及Delete打印作业。核心类是Win32_PrintJob. 的变量printJob来自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)
于 2019-04-28T13:06:33.493 回答