如何通过Printername和JobID暂停Win32_printJob
本文关键字:Win32 printJob 暂停 JobID 何通过 Printername | 更新日期: 2023-09-27 18:05:32
任务
当用户打印文档时,暂停弹出窗口将出现,然后填写表单,点击输入,表单将关闭,作业已恢复。
我们有50台网络打印机、2000客户端机器和一台打印服务器。
每个客户有3或4台打印机
问题
如果用户在本地打印文档(例如:PDF PRINTER、XPS document WRITER((或(使用网络打印机,则打印作业立即暂停。
我的尝试
打印任何文档时,事件侦听器将监视并返回打印作业。
-
在第一次暂停方法中,有时有效,但大多数时间无法正常工作。因为,它正在搜索打印作业,但它不在那里。
-
在第二次暂停方法不起作用因为,事件侦听器返回Managementbaseobject,但如果要暂停打印作业,则需要ManagementObject如何将ManageBaseObject转换为ManageObject
PRINTJOB事件侦听器代码
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;
}
WMI速度太慢。您可能需要使用FindFirstPrinterChangeNotification和FindNextPrinterChangeNotification。
你可能会发现这些例子很有用。
最后,我在MSDN官方文档网站上找到了一个控制打印机的解决方案,下面是链接:https://www.codeproject.com/Articles/6592/A-Simple-Approach-for-Controlling-Print-Jobs-using
说明了如何对Pause
、Resume
和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-对于新的打印作业
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)