473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Monitoring printer-spoolers using WMI

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
Nov 21 '05 #1
3 5088
I'd be inclined to throw some Console.Writeline's into the JobCount method.

I suspect that you might be getting some collisions between you usages.
E.g., 2nd usage hitting the JobQuery.Get() before the first usage has
finished returning Jobs.Count.

Another point could be in the Finally clause. I have never fully understood
how one can do what you have done in the Try clause and disposed of the
source object in the Finally clause and still have the correct value
returned. Sure, I have never seen it fail yet, but I don't entirely trust it
(because I don't fully understand it).

I wonder what would happen if you assigned Jobs.Count to a local integer and
returned that instead.

Just because I'm paranoid, it doesn't mean they're not out to get me.
"Olaf Rabbachin" <Ol*********@IntuiDev.com> wrote in message
news:ej**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2
Hi,

Stephany Young wrote:
I'd be inclined to throw some Console.Writeline's into the JobCount method.
I did that, but it didn't help.
I suspect that you might be getting some collisions between you usages.
E.g., 2nd usage hitting the JobQuery.Get() before the first usage has
finished returning Jobs.Count.
The class I quoted was the simplest I had. I also tried a couple of other
approaches, one i.e. simply having the calling thread sleep for half a
second. Doing so showed each WMI-thread was finished before the next one
was created.
Another point could be in the Finally clause. I have never fully understood
how one can do what you have done in the Try clause and disposed of the
source object in the Finally clause and still have the correct value
returned. Sure, I have never seen it fail yet, but I don't entirely trust it
(because I don't fully understand it).
That's because I'd like to make sure that all objects are disposed of when
exiting the function. How else could you achieve that if not in the
Finally-clause?
I wonder what would happen if you assigned Jobs.Count to a local integer and
returned that instead.


Tried that as well - no change.

Anyway, I found out something else - it seems to be really related to the
printer that I'm not receiving any spooler-related stuff - with some it'll
work and with others it won't. Is there different approaches for different
printers? I don't really see a difference there ... :-(

Cheers,
Olaf
Nov 21 '05 #3
Hi,

Olaf Rabbachin wrote:
Anyway, I found out something else - it seems to be really related to the
printer that I'm not receiving any spooler-related stuff - with some it'll
work and with others it won't. Is there different approaches for different
printers? I don't really see a difference there ... :-(


geez, got it. I was dumb enough to think that the contents of <DriverName>
would always be the printer's name.
So much for trying things out and thinking they'd be right - I tried 4
printers and their <DriverName> always showed the printers' names, but the
one I experienced the problems with was one that had a non-standard driver
which was named differently. Nice that, while testing, I constantly
switched the printers being checked and thus ended up thinking that it was
a matter of two printers in a row being used.

Anyway - instead, I am now using the <Name> item (seems to include the same
information as the <Caption> and <Description> items). Too bad that
property isn't limited to the name (as would've been expected and which was
the reason why I chose to use <DriverName> in the first place) - it
contains a suffix in the form of ", [ID]" where [ID] is the job's ID (also
available via the <JobID>-item). I have no idea as to why it is being
appended - sure doesn't make a whole lot of sense (a bug maybe?).

I'm currently cutting off anything after the last "," which will leave me
with the printer's name.
Also I have to browse all jobs of all printers as the WMI-query doesn't
seem to know LIKE-statements as in regular SQL.

So, unless there's a way (I sure don't know one) to retrieve a printer's
driver-name by the printer's name I'll have to stick with that.

Cheers,
Olaf
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sven Dzepina | last post by:
Hi All, how I can make a simply HostWatch - system which use a mysql DB and send me an e-Mail if the Server goes off and again on? I began to programm this last night, but the MySQL inquiry is...
2
by: Patrick Herb | last post by:
Hello, I'm trying to print the content of a RichTextBox from my VB 6 app. What I want is that the CommonDialog shows up, the user selects a printer and the content of the RichTextBox prints to...
2
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
0
by: Dan | last post by:
Does .NET provide any way to implement printer monitoring (similar to FindFirstPrinterNotification), or is it necessary to call the API?
4
by: johnm | last post by:
Hello, We currently are running a CRM application that uses DB/2 7.2 for the data repository. We will be upgrading to 8.2 later this year....maybe....time and resources permitting. The...
4
by: sengkok | last post by:
hi, i have written a small print job monitoring program which can monitor the job ID and total pages printed . i am facing on strange problem which is the total print pages is always equal to 1 ,...
1
by: Karthic | last post by:
When i right click on the .rpt file in the VS 2003, i see a property printer setting. It says "No printer" on the top and there is option to select printer and paper settings etc.. I want to...
1
by: =?Utf-8?B?UGxheWE=?= | last post by:
Is there a way in VB .NET to check and see if your local printer is out of paper? I have an application on a shop floor and users don't have access to the printer because it is in a secure cabnet,...
1
by: Tomka | last post by:
Hello. Monitoring a print job with all information Windows printer queue serves is no issue. But If I print a page the page is sent from te spooler to the printer. As soon as the spooler has no...
4
drhowarddrfine
by: drhowarddrfine | last post by:
I have a new customer with a cash register that runs on Windows XP. This register software runs full screen and I'm told you can't run anything else while this is running, but I don't know what they...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.