Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / Visual Basic

Monitoring a Printer Queue from VB.NET

Rate me:
Please Sign up or sign in to vote.
4.61/5 (67 votes)
2 May 2014CPOL4 min read 1.6M   18.1K   188  
How to monitor a printer queue from Visual Basic .NET
'\\ --[PrintJobEventArgs]---------------------------------------------
'\\ (c) Merrion Computing Ltd 
'\\     http://www.merrioncomputing.com
'\\ ------------------------------------------------------------------

''' -----------------------------------------------------------------------------
''' Project	 : PrinterQueueWatch
''' Class	 : PrintJobEventArgs
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Class wrapper for the event arguments used in the change events 
''' relating to individual jobs in a printer queue..
''' </summary>
''' <remarks>
''' </remarks>
''' <example>Prints the user name of a job when it is added 
''' <code>
'''   Private WithEvents pmon As New PrinterMonitorComponent
'''
'''   pmon.AddPrinter("Microsoft Office Document Image Writer")
'''   pmon.AddPrinter("HP Laserjet 5")
''' 
'''    Private Sub pmon_JobAdded(ByVal sender As Object, ByVal e As System.EventArgs) Handles pmon.JobAdded
'''
'''    With CType(e, PrintJobEventArgs)
'''        Trace.WriteLine( .PrintJob.Username )
'''     End With
'''
'''  End Sub
''' </code>
''' </example>
''' <history>
''' 	[Duncan]	21/11/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class PrintJobEventArgs
    Inherits EventArgs
    Implements IDisposable

#Region "Private member variables"
    Private _Job As PrintJob
    Private _EventTime As DateTime
    Private _EventType As PrintJobEventTypes
#End Region

#Region "Public enumerated types"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Duncan]	21/11/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Enum PrintJobEventTypes
        JobAddedEvent = 1
        JobDeletedEvent = 2
        JobSetEvent = 3
        JobWrittenEvent = 4
    End Enum

#End Region

#Region "Public Properties"
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The print job for which this event occured
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Duncan]	21/11/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property PrintJob() As PrintJob
        Get
            Return _Job
        End Get
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The date and time at which the print job event occured
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Duncan]	21/11/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property EventTime() As DateTime
        Get
            Return _EventTime
        End Get
    End Property

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The type of job event that occured
    ''' </summary>
    ''' <value></value>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Duncan]	21/11/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public ReadOnly Property EventType() As PrintJobEventTypes
        Get
            Return _EventType
        End Get
    End Property
#End Region

#Region "Public Constructors"
    Friend Sub New(ByVal Job As PrintJob, ByVal Time As DateTime, ByVal EventType As PrintJobEventTypes)
        _Job = Job
        _EventTime = Time
        _EventType = EventType
    End Sub

    Friend Sub New(ByVal Job As PrintJob, ByVal EventType As PrintJobEventTypes)
        Me.New(Job, DateTime.Now, EventType)
    End Sub
#End Region

#Region "Public Methods"
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Frees up any system resources used by this job event notification
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Duncan]	21/11/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub


    Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            _Job.Dispose()
        End If

    End Sub

    Public Overloads Function Equals(ByVal PrintJobEventArgs As PrintJobEventArgs) As Boolean
        If Not PrintJobEventArgs Is Nothing Then
            If PrintJobEventArgs.EventType = _EventType AndAlso PrintJobEventArgs.PrintJob.JobId = _Job.JobId AndAlso PrintJobEventArgs.PrintJob.PrinterName = _Job.PrinterName Then
                Return True
            Else
                Return False
            End If
        End If
    End Function

#End Region

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions