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
dajoebombAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.