Article View: borland.public.cppbuilder.nativeapi
Article #4318Re: Printer notification
From: Remy Lebeau (TeamB)
Date: Fri, 10 Feb 2006 22:57
Date: Fri, 10 Feb 2006 22:57
151 lines
5471 bytes
5471 bytes
"Bogdan" <boprea@xnet.ro> wrote in message
news:43ecfc34$1@newsgroups.borland.com...
> But the thread still terminates cleanly, even with :
> while(!Terminated)
> {
> WaitForSingleObject(Notifier,INFINITE);
> ....
> }
If there are no pending printer notifications, then your thread is blocked
indefinately. The application will not be able to shut the thread down
until another notification arrives, because the thread will not be able to
check its Terminated property. If you don't want to use a second event
object, then you should not be using an INFINITE timeout on
WaitForSingleObject(). Give it a smaller timeout value instead so that the
thread can properly check its Terminated property periodically.
> I've tried using the pPrinterNotifyOptions and ppPrinterNotifyInfo
> options in the following code to look for the number of pages printerd
> so far, but i'm not sure I'm using them corectly...
Try the following code (untested):
#include <map>
struct JobInfo
{
DWORD Status;
DWORD NumPages;
DWORD PagesPrinted;
DWORD NumBytes;
DWORD BytesPrinted;
JobInfo()
{
Status = 0;
NumPages = 0;
PagesPrinted = 0;
NumBytes = 0;
BytesPrinted = 0;
}
};
class TPrinterMonitor : public TThread
{
private:
typedef std::map<DWORD, JobInfo> JobMap;
typedef JobMap::iterator JobMapIter;
HANDLE Printer;
HANDLE Notifier
JobMap Jobs;
void __fastcall ProcessPrinterNotifyInfo(PPRINTER_NOTIFY_INFO pni);
void __fastcall Update();
protected
virtual void __fastcall Execute();
virtual void __fastcall DoTerminate();
public:
__fastcall TPrinterMonitor(HANDLE hPrinter);
};
__fastcall TPrinterMonitor::TPrinterMonitor(HANDLE hPrinter)
: TThread(true)
{
Printer = hPrinter;
Notifier = INVALID_HANDLE_VALUE;
Resume();
}
void __fastcall TPrinterMonitor::Execute()
{
DWORD Changes;
PRINTER_NOTIFY_OPTIONS pno = {0};
pno.Version = 2;
pno.Flags = PRINTER_NOTIFY_OPTIONS_REFRESH;
PPRINTER_NOTIFY_INFO pni;
Notifier = FindFirstPrinterChangeNotification(Printer,
PRINTER_CHANGE_JOB, 0, NULL);
if( Notifier == INVALID_HANDLE_VALUE )
return;
while( !Terminated )
{
if( WaitForSingleObject(Notifier, 1000) == WAIT_OBJECT_0 )
{
Changes = 0;
if( FindNextPrinterChangeNotification(Notifier, &Changes,
&pno, (void**)&pni) )
{
ProcessPrinterNotifyInfo(pni);
FreePrinterNotifyInfo(pni);
}
}
}
void __fastcall TPrinterMonitor::DoTerminate()
{
if( Notifier != INVALID_HANDLE_VALUE )
FindClosePrinterChangeNotification(Notifier);
}
void __fastcall
TPrinterMonitor::ProcessPrinterNotifyInfo(PPRINTER_NOTIFY_INFO pni)
{
for(DWORD x = 0; x < pni->Count; ++x)
{
PRINTER_NOTIFY_INFO_DATA &Data = pni->nData[x];
if( Data.Type == JOB_NOTIFY_TYPE )
{
switch( Data.Field )
{
case JOB_NOTIFY_FIELD_STATUS:
if( (Data.adwData[0] == JOB_STATUS_DELETED )
Jobs.erase(Data.Id);
else
Jobs[Data.Id].Status = Data.adwData[0];
break;
case JOB_NOTIFY_FIELD_PAGES_PRINTED:
Jobs[Data.Id].PagesPrinted = Data.adwData[0];
break;
case JOB_NOTIFY_FIELD_TOTAL_BYTES:
Jobs[Data.Id].Info.NumBytes = Data.adwData[0];
break;
case JOB_NOTIFY_FIELD_BYTES_PRINTED:
Jobs[Data.Id].Info.BytesPrinted = Data.adwData[0];
break;
}
}
}
Synchronize(Update);
}
void __fastcall TPrinterMonitor::Update()
{
// update the UI with the info of the current Jobs as needed ...
}
Gambit
Technical Details
Message-ID:
<43ed198a$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<43ecd5bb@newsgroups.borland.com> <43ecf638$1@newsgroups.borland.com> <43ecfc34$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,2eb2d0dc0d96a498 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <43ecd5bb@newsgroups.borland.com> <43ecf638$1@newsgroups.borland.com> <43ecfc34$1@newsgroups.borland.com> Subject: Re: Printer notification Date: Fri, 10 Feb 2006 14:57:06 -0800 Lines: 153 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <43ed198a$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1139612042 68.69.17.41 (10 Feb 2006 14:54:02 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail