title: Print preview performance
body: <p>My question is pretty straightforward. I need to display a print preview dialog in a multi-user ERP environment. It speaks for itself that printing should be as fast as possible.
  However, if I use the code below, it takes about 10! seconds before the preview is displayed and is fully generated. This is without database access or any other CPU intensive operations.</p>
  
  <p>Is there any way or method to improve this performance? I'm also able to use WPF, should that be necessary.</p>
  
  <p>I've noticed that if you generate the preview, close it, and then quickly generate it again, it responds much faster, about a second or two. If you then wait another 5 seconds or so, generate it again, it takes about 10 seconds again. 
  I'm guessing some type of caching is going on, but don't have a clue what's actually happening.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.Drawing.Printing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace PrintPerformanceTests
  {
      public class PrintPreviewTest
      {
          private string printerName;
          private PrintPreviewDialog printPreviewDialog1;
          private PrintDocument printDocument1 = new PrintDocument();
          private Random random = new Random();
  
          public string PrinterName
          {
              get { return printerName; }
              set { printerName = value; }
          }
  
          public PrintPreviewDialog PrintPreviewDialog
          {
              get { return printPreviewDialog1; }
              set { printPreviewDialog1 = value; }
          }
  
          public PrintPreviewTest(string printerName, PrintPreviewDialog printPreviewDialog)
          {
              this.PrinterName = printerName;
              this.PrintPreviewDialog = printPreviewDialog;
              printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
          }
  
          public void GenerateRandomPrintPreview()
          {
              Cursor.Current = Cursors.WaitCursor;
              try
              {
                  PrintPreviewDialog.Document = printDocument1;
                  PrintPreviewDialog.ShowDialog();
              }
              catch (Exception exc)
              {
                  Cursor.Current = Cursors.Default;
                  MessageBox.Show(exc.ToString());
              }
              finally
              {
                  Cursor.Current = Cursors.Default;
              }
          }
  
          void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
          {
              e.Graphics.PageUnit = GraphicsUnit.Millimeter;
              using(Font f = new Font("Arial", 10f))
              {
                  for (int i = 0; i &lt; 20; i++)
                  {
                      string txt = "Random string " + i.ToString();
                      e.Graphics.DrawString(txt, f, Brushes.Black, new PointF(random.Next(10, 200), random.Next(10,280)));
                  }
              }
  
              e.HasMorePages = false;
          }
      }
  }
  </code></pre>
  
  <p>usage:</p>
  
  <pre><code>        PrintPreviewTest pt = new PrintPreviewTest(tbPrinter.Text, printPreviewDialog);
          pt.GenerateRandomPrintPreview();
  </code></pre>