Performance : PrintDocument / PrinterSettings / PaperSizes is very VERY **slow**

R

Robert Hooker

Hi,

I'm curious to know if I'm doing something wrong here, or if this is just
mind-numbingly slow for a reason.

In a simple WindowsFormsApplication:

public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();

PrintDocument printDoc = new PrintDocument();
// Add each papersize name (string) to an array
//
// Querying printer for all its papersizes is **Slow**
// - This loop takes about 8 seconds (!!) to execute on my machine for 14
sizes
ArrayList names = new ArrayList();
PrinterSettings printer = printDoc.PrinterSettings;
for( int i=0; i<printer.PaperSizes.Count; i++ )
{
PaperSize psize = printer.PaperSizes;
names.Add(psize.PaperName);
}
}

It appears that each call to printer.PaperSizes.xxxxx takes a very VERY long
time. I can speed that loop up by moving the printer.PaperSizes.Count up out
of the loop, but having to do this seems funky.

Am I missing something, or is this stuff just broken for speed?

Rob.
 
J

Jon Skeet

It appears that each call to printer.PaperSizes.xxxxx takes a very VERY long
time. I can speed that loop up by moving the printer.PaperSizes.Count up out
of the loop, but having to do this seems funky.

Am I missing something, or is this stuff just broken for speed?

You're retrieving printer.PaperSizes twice in each iteration of the
loop. If you fetch the PaperSizes property just once, it speeds things
up considerably. Here's a neat way to do it:

PrinterSettings printer = printDoc.PrinterSettings;
foreach (PaperSize psize in printer.PaperSizes)
{
names.Add(psize.PaperName);
}
 
Y

Yan-Hong Huang[MSFT]

Hi Robert,

Thanks for posting in the group.

I noticed that the question is also posted in dotnet.framework group. If
you have time, please check my reply there. I will follow up in that group.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: "Robert Hooker" <[email protected]>
!Subject: Performance : PrintDocument / PrinterSettings / PaperSizes is
very VERY **slow**
!Date: Fri, 26 Sep 2003 09:29:35 -0600
!Lines: 36
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <[email protected]>
!Newsgroups:
microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsf
orms,microsoft.public.dotnet.languages.csharp
!NNTP-Posting-Host: 12.155.152.130
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
!Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:53212
microsoft.public.dotnet.languages.csharp:187556
microsoft.public.dotnet.framework:54823
!X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
!
!Hi,
!
!I'm curious to know if I'm doing something wrong here, or if this is just
!mind-numbingly slow for a reason.
!
!In a simple WindowsFormsApplication:
!
!public Form1()
!{
! // Required for Windows Form Designer support
! InitializeComponent();
!
! PrintDocument printDoc = new PrintDocument();
! // Add each papersize name (string) to an array
! //
! // Querying printer for all its papersizes is **Slow**
! // - This loop takes about 8 seconds (!!) to execute on my machine for 14
!sizes
! ArrayList names = new ArrayList();
! PrinterSettings printer = printDoc.PrinterSettings;
! for( int i=0; i<printer.PaperSizes.Count; i++ )
! {
! PaperSize psize = printer.PaperSizes;
! names.Add(psize.PaperName);
! }
!}
!
!It appears that each call to printer.PaperSizes.xxxxx takes a very VERY
long
!time. I can speed that loop up by moving the printer.PaperSizes.Count up
out
!of the loop, but having to do this seems funky.
!
!Am I missing something, or is this stuff just broken for speed?
!
!Rob.
!
!
!
 

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