1

I am trying to monitor a print queue using a Observable Collection however when an item is added to the printer queue it does not updated. am I missing something. Here is my code so far.

Imports System.Printing
Imports System.Text
Imports System.IO
Imports System.Collections
Imports System.Management
Imports System.Drawing.Printing
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized

Public Class Form1
    Dim localPrintServer2 As LocalPrintServer
    Dim defaultPrintQueue2 As PrintQueue
    Dim listSentToPrinter As New List(Of String)()
    Dim listPrinterQueue As New List(Of String)()
    Dim listJobsInQueue As New List(Of String)()
    ' Private m_queueObList As New ObservableCollection(Of String)
    Public queueObList As New ObservableCollection(Of String)

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
             AddHandler queueObList.CollectionChanged, AddressOf Me.OnCollectionChanged
    End Sub

    Private Sub OnCollectionChanged(sender As Object, e As NotifyCollectionChangedEventArgs)
        Try

        Dim obsSender As ObservableCollection(Of String) = TryCast(sender, ObservableCollection(Of String))
        Dim editedOrRemovedItems As New List(Of String)()

           getPrintQueue()


        If e.Action = NotifyCollectionChangedAction.Add Then
            MsgBox("Item Added")
            'search listSentToPrinter

        End If

        If e.Action = NotifyCollectionChangedAction.Remove Then
            MsgBox("Item Removed")
        End If

              'Label1.Text = queueObList.Count

        Dim action As NotifyCollectionChangedAction = e.Action

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub getPrintQueue()
        Try

            localPrintServer2 = New LocalPrintServer()
            defaultPrintQueue2 = LocalPrintServer.GetDefaultPrintQueue()
            Timer1.Enabled = True
            Timer1.Interval = 50
            Dim jobs As PrintJobInfoCollection = defaultPrintQueue2.GetPrintJobInfoCollection
            For Each job As PrintSystemJobInfo In jobs
                'listPrinterQueue.Add(job.Name)
                queueObList.Add(job.Name)

                Label1.Text = queueObList.Count
                'lstPrinterQueue.Items.Add(job.Name & "  " & job.JobStatus)
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Iletarerate()
        ListBox1.Items.Clear()
        For Each item As String In queueObList
            ListBox1.Items.Add(item)
        Next
    End Sub


    Public Sub FindJobInQueue(ByVal item As String)
        If listJobsInQueue.Contains(item) Then

        Else

            If (listPrinterQueue.Count >= 1) Then
                If listPrinterQueue.Contains(item) Then
                    lstJobInQueue.Items.Add("Found  " & item)
                    listJobsInQueue.Add(item)
                    'Update stutus if successfully updated
                    listSentToPrinter.Remove(item)
                End If
            Else
                ' list is empty
            End If
        End If
    End Sub



    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        queueObList.RemoveAt(0)
        Iletarerate()
    End Sub
End Class

The code works to add everything to the observable collection which is in the queue. The buttons work to add and remove items. The actual print queue does not update the collection

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574

0 Answers0