Multiple threads using a shared printer resource

G

Gregory Gadow

I've cobbled together a PrinterClass that takes a text file and dumps it
to a printer. The app using is has multiple threads, all of which need
access to a shared instance. Can someone point me to an example of
multiple threads synchronized to use a single shared resource?
 
C

Chris

Gregory said:
I've cobbled together a PrinterClass that takes a text file and dumps it
to a printer. The app using is has multiple threads, all of which need
access to a shared instance. Can someone point me to an example of
multiple threads synchronized to use a single shared resource?

You are not sending it to a windows printer queue? Usually windows takes
care of this for you.

If you are doing something funny you should think about using one thread
to do the job and having a queue that holds the jobs. All the worker
threads just can add to the queue, the printer thread then processes the
queue.

Chris
 
G

Gregory Gadow

Chris said:
You are not sending it to a windows printer queue? Usually windows takes
care of this for you.

If you are doing something funny you should think about using one thread
to do the job and having a queue that holds the jobs. All the worker
threads just can add to the queue, the printer thread then processes the
queue.

The whole class is a bit much to cut-n-paste, but basically, it is built
around this declaration:

Private Shared WithEvents pd as PrintDocument

The public function that invokes the class is

Public Function SendFileToPrinter(ByVal ThisFile As String, ByVal ThisPrinter
As String) _
As Boolean

which creates a new instance of pd (if necessary) and sets up margins,
Landscape orientation, and tells pd what printer to use. Then, it opens the
text file using a shared instance of StreamReader and calls pd.Print(). The
..NET library takes over and, at the start of each new page of text, calls
pd_PrintPage by raising the PrintPage event on pd.

pd_PrintPage is a private shared sub handling the PrintPage event of the pd
object. It looks to the instance of StreamReader to fetch the next line of the
text document, then calls Graphics.DrawString to draw the text to the printer.
The routine then reads the next line, looping until either a maximum number of
lines has been read, a form feed character is encountered or the end of file
is reached. When all of the pages have been printed (indicated by setting
HasMorePages = False in the event parameter, ev As PrintPageEventArgs), the
call to pd.Print() returns. SendFileToPrinter then ties up any loose ends and
returns with true if there were no errors or false if there were.

Yes, all I need is to dump a text file to a network printer, but that seems to
be impossible under .NET 2.0 :-/

Anyway, this class worked great until I went to a threaded version of a
service I've written. I need to figure out how to make PrinterClass
thread-safe and allow only one thread to use it at a time. I don't think
allowing multiple instances of the class would work, as you will still have
competing processes trying to control the one physical printer. As now
written, all of the class internals are shared, both the declares and the two
methods.
 
C

Chris

Gregory said:
Chris wrote:




The whole class is a bit much to cut-n-paste, but basically, it is built
around this declaration:

Private Shared WithEvents pd as PrintDocument

The public function that invokes the class is

Public Function SendFileToPrinter(ByVal ThisFile As String, ByVal ThisPrinter
As String) _
As Boolean

which creates a new instance of pd (if necessary) and sets up margins,
Landscape orientation, and tells pd what printer to use. Then, it opens the
text file using a shared instance of StreamReader and calls pd.Print(). The
.NET library takes over and, at the start of each new page of text, calls
pd_PrintPage by raising the PrintPage event on pd.

pd_PrintPage is a private shared sub handling the PrintPage event of the pd
object. It looks to the instance of StreamReader to fetch the next line of the
text document, then calls Graphics.DrawString to draw the text to the printer.
The routine then reads the next line, looping until either a maximum number of
lines has been read, a form feed character is encountered or the end of file
is reached. When all of the pages have been printed (indicated by setting
HasMorePages = False in the event parameter, ev As PrintPageEventArgs), the
call to pd.Print() returns. SendFileToPrinter then ties up any loose ends and
returns with true if there were no errors or false if there were.

Yes, all I need is to dump a text file to a network printer, but that seems to
be impossible under .NET 2.0 :-/

Anyway, this class worked great until I went to a threaded version of a
service I've written. I need to figure out how to make PrinterClass
thread-safe and allow only one thread to use it at a time. I don't think
allowing multiple instances of the class would work, as you will still have
competing processes trying to control the one physical printer. As now
written, all of the class internals are shared, both the declares and the two
methods.

I still think if load your print documents into a queue and let a thread
that just processes the queue pull stuff out of the queue, you'd solve
your problem.

Then again, since you are just sending it to the windows print queue,
I'd think it'd work anyways.

Chris
 
G

Gregory Gadow

Chris said:
I still think if load your print documents into a queue and let a thread
that just processes the queue pull stuff out of the queue, you'd solve
your problem.

Then again, since you are just sending it to the windows print queue,
I'd think it'd work anyways.

The only documentation I was able to find on how to print from .NET is encapsulated
in the PrinterClass I've described above. If you could point me to information on
how to dump a text file to a printer queue in a thread-safe manner, I would be
grateful.
 

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