• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1010
  • Last Modified:

How to run method when a job is added to the print queue

Hi guys,
            Basically ive made a program to delete duplicate items in the print queue. The only problem is that you either have to have it on button click or on a timer. I dont really want this i want it to just do the code when a new job is submitted. Im using .net framework 3. Thanks for any replies!
            Joe
0
dajoebomb
Asked:
dajoebomb
  • 5
  • 4
1 Solution
 
athapaCEO / CTOCommented:
Try this component. I evaluated this components several years back and it seemed to work good. Now it is open source too.
http://www.merrioncomputing.com/Download/PrintQueueWatch/index.htm
The component will notify you of various printer events such as JobAdd, JobWritten, etc.

I ended up writing my own in my case because I wanted to do some extra stuff but this should do what you need.
0
 
dajoebombAuthor Commented:
Thanks thats just what im looking for, having a few problems getting the rest of the code to work though, but im sure if i keep going ill get it eventually!
0
 
dajoebombAuthor Commented:
could anyone tell me why this code doesnt work please, i get an error about cant access it becuas its on a different thread. thanks
Imports System.printing
Public Class Form1
 
    Dim localPrintServer As New LocalPrintServer()
    Dim q As PrintQueue = localPrintServer.GetDefaultPrintQueue()
 
 
    Private Sub PrinterMonitorComponent1_JobAdded(ByVal sender As Object, _
          ByVal e As EventArgs) Handles PrinterMonitorComponent1.JobAdded
        Dim pj As PrintSystemJobInfo
 
        If isInQueue(pj) Then
            pj.Cancel()
        End If
 
 
 
    End Sub
 
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PrinterMonitorComponent1.AddPrinter("Brother HL-7050 series")
    End Sub
 
 
    Private Function isInQueue(ByVal pj As PrintSystemJobInfo)
        Dim boolres As Integer
        For Each pj1 In q.GetPrintJobInfoCollection()
            If pj.JobIdentifier <> pj1.JobIdentifier Then
                boolres = pj.JobName.CompareTo(pj1.JobName)
                If boolres = 0 Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        Next
    End Function
 
End Class

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
athapaCEO / CTOCommented:
Which section of the code is throwing exception?
If the problem is caused by monitoring thread trying to update UI then you'll have to use Control.InvokeRequired  (put your Control or Form) and Invoke the call in UI thread.
0
 
dajoebombAuthor Commented:
For Each pj1 In q.GetPrintJobInfoCollection()
Its this line here,
0
 
athapaCEO / CTOCommented:
can you add try/catch and paste your exception
0
 
dajoebombAuthor Commented:
System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
   at System.Windows.Threading.Dispatcher.VerifyAccess()
   at System.Windows.Threading.DispatcherObject.VerifyAccess()
   at System.Printing.PrintQueue.GetPrintJobInfoCollection()
   at PrintMonitor.Form1.isInQueue(PrintSystemJobInfo pj) in C:\Documents and Settings\adjh\Local Settings\Application Data\Temporary Projects\PrintMonitor\Form1.vb:line 29


Thanks for your help and replies athapa
0
 
athapaCEO / CTOCommented:
See if this works

   'create a delegate
   private delegate function isInQueueDelegate(ByVal pj As PrintSystemJobInfo) as boolean

   'change isinqueue like this
Private Function isInQueue(ByVal pj As PrintSystemJobInfo)
        Dim boolres As Integer
        if me.InvokeRequired then
              dim d as new isinQueueDelegate(AddressOf isinQueue)
              return me.invoke(d, new object[]{pj})
        else

        For Each pj1 In q.GetPrintJobInfoCollection()
            If pj.JobIdentifier <> pj1.JobIdentifier Then
                boolres = pj.JobName.CompareTo(pj1.JobName)
                If boolres = 0 Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        Next

        endif

    End Function


0
 
dajoebombAuthor Commented:
Great that stopped the error!! Only thing is that the print job does not delete for some reason, i will check my code again!
 Thank you very much athapa
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now