Word::Document PrintOut Async issues

N

NickP

Hi there,

How would I wait until the Word document has finished printing to file?
I have tried several attempts so far, firstly to monitor the printer in
question to wait for the state to change from Busy, and secondly to monitor
to file for the lock to be released. Both of which are unsuccessful as it
locks the thread that is currently attempting to Print. This suggests to be
that even though Word returns the second the document starts to print or
fails, it is not truely asynchronous.

I have code working via VB.NET that just monitors the lock on the file,
and this works fine, but not in the case of C++.

Any ideas on how to handle this? I need my function to only return once
the document has printed successfully.

.....

if(S_OK==pDPrDocument->PrintOut(&vtMissing, &pVarAppend, &vtMissing,
&pVarOutputFileName, &vtMissing, &vtMissing, &vtMissing, &vtMissing,
&vtMissing, &vtMissing, &pVarPrintToFile))
{
bool pBlnBusy = true;
HANDLE pHanPrinter = NULL;
if(OpenPrinter(_T("<name of printer here>"), &pHanPrinter, NULL))
{
while(pBlnBusy)
{
Sleep(100);
DWORD pDWdSize;
GetPrinter(pHanPrinter, 6, NULL, NULL, &pDWdSize);
PRINTER_INFO_6* pPIoStatus = (PRINTER_INFO_6 *)GlobalAlloc(GPTR,
pDWdSize);
GetPrinter(pHanPrinter, 6, (LPBYTE)pPIoStatus, pDWdSize,
&pDWdSize);
bool pBlnBusyTemp = false;
int pIntCurInfo;
for(pIntCurInfo=0;pIntCurInfo!=pDWdSize;pIntCurInfo++)
{
if(pPIoStatus[pIntCurInfo].dwStatus)
pBlnBusyTemp = true;
}
if(!pBlnBusyTemp) pBlnBusy = false;
}
ClosePrinter(pHanPrinter);
}
}

.....

Unfortunately the above still locks the thread that is attempting to
print. Even throwing in a messagebox doesn't help.

Thanks a million in advance.

Nick.
 
N

NickP

Hi there,

Just to add to this, I've just done it using a large document, 300~
pages. It appears that the method is Synchronous after all and that it
appears to be a slight delay in UI updates to printer status that I am
experiencing.

Nick.
 
J

Jialiang Ge [MSFT]

Hello Nick,

From your post, my understanding on this issue is: you want to know how to
be notified when a word document finishes printing. If I'm off base, please
feel free to let me know.

According to your description, you are using polling to get notified when a
document is printed. In terms of the KB:
http://support.microsoft.com/kb/q196805/, On Windows NT, a printer queue
can be monitored by polling or by change events generated by the printer
queue. On Windows, it can only poll because the change notification
interface is not implemented. Here, 'Windows NT' refers to Windows NT,
2000, XP, 2003, etc and 'Windows' refers to Windows 98, Windows Me.
Therefore, if your operating system is Windows NT, I think it is better to
monitor print queue by events because of the performance gain compared with
Polling. (See the section 'Monitoring Print Queue by Events' in the above
KB article, the API 'FindFirstPrinterChangeNotification' in
http://msdn2.microsoft.com/en-us/library/ms536031.aspx and the API
'FindNextPrinterChangeNotification' in
http://msdn2.microsoft.com/en-us/library/ms535481.aspx).

No matter whether you are using Polling or using change Events, your
current thread will be blocked if those operations are not put in a new
thread. Below is an analysis of the sample project 'PrintMon' available in
the kb http://support.microsoft.com/kb/q196805/

The PrintMon uses 'Polling' by default. If the item 'Use Printer
Notification' in the menu 'Options' is selected, the application will start
to use 'change Events'.

When user input a printer to be monitored in the menu 'Printer' and click
OK, the method 'OnSelectPrinter' in PrintJob.C will be called. In the
'OnSelectPrinter', it will open the specified printer and call
'StartPolling' or 'StartNotification' according to user's selection in
'Options'. Suppose that we selected the 'Use Printer Notification', then
the current thread goes to the function 'StartNotifications' in Threads.C,
in which you may find the following codes:

hThread = (HANDLE)_beginthreadex(NULL,0,NotificationUpdate,(void
*)hParam,1,&ThreadID);

_beginthreadex is to create a new thread so that the current thread will
not be blocked. This new thread calls 'NotificationUpdate' method in
Threads.C where you will find the check of printer's change events: the
call of FindFirstPrinterChangeNotification and
FindNextPrinterChangeNotification. If we capture the desired state change,
we call 'RefreshFromNotification' defined in PrintJob.C to update the
current thread's information.

You may read the sample's codes by yourself and have a clearer picture of
how it works. If you insist on the Polling method, please refer to
StartPolling instead of StartNotification. Hope it helps.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hi Nick,

Would you mind letting me know the result of the suggestions? If you need
further assistance, feel free to let me know. I will be more than happy to
be of assistance.

Have a great day!

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
N

NickP

Hi Jialiang,

My apologies for the delay in my reply. I ended up enumerating the jobs
of the printer, then finish up once no jobs are left. The reason this is
effective for me is the printer gets deleted straight after anyway so it
*should* only be doing 1 job at a time, and can only be operated via code
anyway.

Thanks for your suggestion though, If I have any issues with the way I
am currently doing it, I shall try the method you have suggested.

Many thanks for your time and help.

Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top