How to pause the Win32_printJob by Printername and JobID

3.5k views Asked by At

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;
        } 
2

There are 2 answers

12
Nick Westgate On

WMI is too slow for this. You probably need to use FindFirstPrinterChangeNotification and FindNextPrinterChangeNotification.

You might find these examples useful.

0
M.Bonjour On

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)