Printing from Service's thread

Hi Experts,

I have a Service Application written in Delphi 6. I need to print a page to the default printer, but this has to be done in a thread.
Procedure doprint;
Begin
 Printer.BeginDoc;
 Printer.Canvas.TextOut(x,y, 'My text to print');
 Printer.EndDoc;
End;
This seems to be ok, but sometimes, printing stops. Exactly, the document is put into the print spooler's queue, but never printed out. If I delete the documents from the print queue it works again.

I think the problem is due the fact, the Canvas object is not thread-safe, so I've put it into synchronize like this:
Synchronize(doprint);
The problem still exists. Delphi's help tells that, the method called with synchronize will be executed in the main VCL thread.
But I have not VCL thread, I have a service which is also a thread...
Moreover, I have no error message or exception at all...


spbltdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
This could be a security related issue. Personally, I've never tried to print from a service anyway, because then I would need to provide a user account for this service and make sure that account has access to at least a default printer. I'm not even sure if a service actually has any real access rights to the printer in the first place.

About synchronizations, this is required if you're working with a GUI-based application that depends on the Forms unit. When you call the Synchronize method of a thread, it will actually send a Windows message to the main thread. But a service doesn't have a message handler thus this would just fail. You would have to use a critical section to make sure printer access is synchronized.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spbltdAuthor Commented:
Finally we have found the problem, it was a hardware failure.

But your comments about the synchronization is still useful, so I accept your answer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.