Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

c# - How to pause the Win32_printJob by Printername and JobID

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(@"
ootcimv2");
    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;
        } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

You might find these examples useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...