473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Multiple threads using a shared printer resource

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
4 1956
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
2
by: Oenone | last post by:
I'm upgrading a DLL from VB6 to VB.NET. The DLL gets called from an ASP.NET web application. In the VB6 code there is a module-level object which stores the context about what the user is doing...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
14
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
8
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...
1
by: sathyp | last post by:
Public Function SetPrinterDefaultsW(ByVal sPrinterName As String, _ ByVal nPaperSize As Long, ByVal nOrientation As Long) As Boolean Dim Prn As Printer Dim hPrinter As Long Dim pd As...
12
by: Dave | last post by:
Hi all, I have an application which has some worker threads which often have to stop and wait for some further information from other threads. These pauses will often take a long time (a couple...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.