Click to See Complete Forum and Search --> : While ..... Wend


John G Duffy
October 23rd, 2001, 08:54 AM
I have a sample class module that has a While Wend loop in it that I can see no way to break out of and I am curious as to how this thing can be functional.
Here is the Routine. Someone here on this forum may recognize this snippet.
Hope you are not offended.

If mhPrinter = 0 then
'\ Need to get the printer handle for the printer device named
lRet = OpenPrinter(newName, mhPrinter, pDef)
'\ and start printer notification chain...
If mChangeNotificationHandle <> 0 then
Call FindClosePrinterChangeNotification(mChangeNotificationHandle)
End If
With pNotifyOptions
.Version = 2
End With
mChangeNotificationHandle = FindFirstPrinterChangeNotification(mhPrinter, PRINTER_CHANGE_ALL, 0, pNotifyOptions)
While true
DoEvents
If WaitForSingleObject(mChangeNotificationHandle, WAIT_REASONABLE) = STATUS_WAIT_0 then
RaiseEvent ChangeNotification
mChangeNotificationHandle = FindNextPrinterChangeNotification(mChangeNotificationHandle, pdwChange, pNotifyOptions, vbNull)
End If
Wend
End If




The While True ... Wend loop seems never to be satisfied. I can see no way to Satisfy the "True" expected condition. I realize DO ... Loop can perform similar functions but still my curiosity is peaked here.

John G

Clearcode
October 23rd, 2001, 09:22 AM
That looks familiar

While..True and Do..Loop are functionally identical. Most people use the latter because it is what they are used to but I cut my teeth on a langauge that didn't have Do..Loop and old habits die very hard (I still write "On Local Error Goto" for example)

The loop is intended to be infinite, although the newest version of the printer watch application has a global boolean variable called bCloseApplication which you can set to true to exit the loop and clean up after it.

The key to how this works is the DoEvents command. This only returns when all triggered events have been serviced. So, if the printer has had an even the ChangeNotification event is triggered and the next time the DoEvents is encountered the event is processed.

However, in the course of developing this code I got a bit stuck because WaitForSingleObject blocks the thread it is running on until it fires or times out. This is not desireable so the latest version of the code uses a server application which just performs that loop and notifies the client when a printer event occurs. Thsi allows the client to continue operating normally while watching the printer queue. This is performed using SendMessage to perform the inter process communication as documented at http://www.merrioncomputing.com/Programming/IPC.htm

HTH,
Duncan

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.

John G Duffy
October 23rd, 2001, 09:38 AM
Duncan,
Thanks. That is what I thought but I did not understand the purpose of the hard loop.

John G