By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Multiple threads using a shared printer resource

P: n/a
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?
--
Gregory Gadow
Feb 6 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Gregory Gadow wrote:
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?
--
Gregory Gadow


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
Feb 6 '06 #2

P: n/a
Chris wrote:
Gregory Gadow wrote:
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?
--
Gregory Gadow


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.
--
Gregory Gadow

Feb 6 '06 #3

P: n/a
Gregory Gadow wrote:
Chris wrote:

Gregory Gadow wrote:
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?
--
Gregory Gadow


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.
--
Gregory Gadow


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
Feb 6 '06 #4

P: n/a
Chris wrote:
Gregory Gadow wrote:
Chris wrote:

Gregory Gadow wrote:

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?
--
Gregory Gadow

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.
--
Gregory Gadow


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.
--
Gregory Gadow
Feb 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.