Hi folks,
during printing, I'm constantly checking the printer-spooler to monitor
whether a document has showed up in a printer's spooler and - after it has
- whether the print-job has been finished.
I'm using the following code for that:
--- 8< ---
Imports System.Management
Public Class CPrinterQueue
Public Shared Function JobCount(ByVal strPrinterName As String) As Integer
Dim strQuery As String = "SELECT * FROM Win32_PrintJob " & _
"WHERE DriverName = '" & strPrinterName.Replace("'", "''") & "'"
Dim JobQuery As New ManagementObjectSearcher(strQuery)
Dim Jobs As ManagementObjectCollection = JobQuery.Get()
Try
Return Jobs.Count
Catch ex As Exception
Return -1
Finally
Jobs.Dispose()
JobQuery.Dispose()
End Try
End Function
end class
--- 8< ---
As long as I'm only using one printer, everything's fine. But after a set
of documents has been printed, a receipt will be printed to a different
printer. As soon as I use the above class/function with the second printer,
it will return a wrong number of jobs upon first call and the code will
just stop running (at "Return Jobs.Count") afterwards.
If I comment out one of the usages (that is, I'm only monitoring one
printer), everything will be fine.
Any pointers?
Thanks!
Cheers,
Olaf