任务
当用户打印文档时,将出现暂停弹出然后填写表格单击输入表格将关闭并且工作已恢复。
我们有 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;
}