Printing from Service's thread

Posted on 2004-11-03
Last Modified: 2010-04-05
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;
 Printer.Canvas.TextOut(x,y, 'My text to print');
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:
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...

Question by:spbltd
    LVL 17

    Accepted Solution

    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.

    Author Comment

    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

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now