问题
TASK
When user print the document ,pause the pop will appear then fill the form click enter the form will closed and job has been resumed.
We have 50 Network Printers , 2000 Client Machine and one print server.
EACH CLIENT had 3 or 4 printers
PROBLEM
If user print the document locally (EX:PDF PRINTER, XPS DOCUMENT WRITER) (or) using network printer (CANON,HP) the print-job was immediately PAUSE.
MY TRIES
When print any of the document the event listener watching and return the print job.
In First Pause Method sometimes work and mostof time doesn't work properly.Because, Its searching for the printjob but its not there is already printed.
In Second Pause Method doesn't work Because,event listener return the Managementbaseobject but If want to pause the Print Job need ManagementObject How to convert ManageBaseObject to 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();
MAIN ACTION CODE
private void printJobArrived_EventArrived(object sender, EventArrivedEventArgs e)
{
try
{
CurrentprintJob = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
}
catch(Exception ex){
}
}
TO PAUSE THE PRINTJOB METHOD 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;
}
TO PAUSE THE PRINTJOB METHOD 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;
}
回答1:
WMI is too slow for this. You probably need to use FindFirstPrinterChangeNotification and FindNextPrinterChangeNotification.
You might find these examples useful.
回答2:
Finally, I've found a solution on MSDN official documentation website for controlling printer, here is the link: https://www.codeproject.com/Articles/6592/A-Simple-Approach-for-Controlling-Print-Jobs-using
It shows that how to Pause
, Resume
and Delete
a print job. The core class is Win32_PrintJob
.
The variable of printJob
is from ManagementObjectSearcher
object which searched one printer instance.
printJob.InvokeMethod("Pause", null);
printJob.InvokeMethod("Resume", null);
printJob.Delete()
I still found that its Python implementation of code : 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)
来源:https://stackoverflow.com/questions/39303965/how-to-pause-the-win32-printjob-by-printername-and-jobid