473,769 Members | 5,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Manageme nt

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 ManagementObjec tSearcher(strQu ery)
Dim Jobs As ManagementObjec tCollection = JobQuery.Get()

Try
Return Jobs.Count
Catch ex As Exception
Return -1
Finally
Jobs.Dispose()
JobQuery.Dispos e()
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 5117
I'd be inclined to throw some Console.Writeli ne'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*********@In tuiDev.com> wrote in message
news:ej******** ******@TK2MSFTN GP12.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.Manageme nt

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 ManagementObjec tSearcher(strQu ery)
Dim Jobs As ManagementObjec tCollection = JobQuery.Get()

Try
Return Jobs.Count
Catch ex As Exception
Return -1
Finally
Jobs.Dispose()
JobQuery.Dispos e()
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.Writeli ne'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
2026
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 too hard for me! =( Thats my code: ----
2
22312
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 the selected printer. I'm not concerned about the text format. What I tried is something like this CommonDialog1.ShowPrinter
2
2599
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 of classes that allow me to interact with network devices. In other words, if I have a path and filename inside of a string variable in python, how do I send the file to the printer?? Thanks in advance
0
1135
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
2239
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 database is currently being hosted with a managed service provider (MSP) at a remote datacenter. This service provider is supposed to be monitoring the health and performance of the database as well as performing proactive maintenance on a regularly...
4
8691
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 , not matter how many pages i have printing . anyone have facing the same issue ? and any solution on this issue ? the code i written as bellow timer1.Stop () ;
1
12181
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 know how will this affect a report rendered by a crystal viewer. I have a web page with a crystal viewer rendering the report on the page. I build my application in one of my development machines and deploy it
1
2798
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, however they don't know when it runs out of paper and they keep pressing the print button. I want to program it so they can't print if it runs out of paper and am not sure if this is possible. Any help would be appriciated. Thanks in advance.
1
3219
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 more data, it reports "print ok" although the printer has a paper jam for example. Does anyone know a generic interface how to control the status of printers (not manufacturer specific)? Thanks for answers.
4
3203
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 mean by that exactly. I doubt they can block any background programs that are started ahead of time. Anyway, this register is always connected to the internet and I'd like to write/use something to monitor the 'net on some port for incoming data...
0
9583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10039
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.