Jason

Jason

  • NA
  • 13
  • 0

PrintDocument.Print() process handles

Jul 15 2009 5:33 PM
Greetings wise and wonderful C# people!

I am trying to determine why my print process is creating so many process handles.  By stepping through the code in debug mode (VS 2008) and watching Task Manager I have narrowed it down to the call to PrintDocument.Print().  After stepping into (F11) or over (F10) this method call the number of process handles for my application jump up by 800 handles and they never seem to be released.  Below is my class definition.

Any insight into what the PrintDocument.Print() method is doing and why it creates so many handles would be greatly appreciated.  Thanks!

The code that ends up using this class looks kind of like this:

Method1()
{
        if (someCondition == true)
        {
              System.Threading.Thread p = new System.Threading.Thread(new   System.Threading.ParameterizedThreadStart(Method2));
              p.Start((object)t);
        }
}

Method2
{
       Print prnt = new Print();
       string printString = "";
       // create the string to print
      
       prnt.PrintData(printString);
}


// Print class definition
//thanks MSDN for most of this code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;

namespace SomeProject
{
    class Print
    {
        private StreamReader streamToPrint;
        private MemoryStream buff;
        private Font printFont;

        public Print()
        {
            streamToPrint = null;
            buff = null;
            printFont = new Font("Arial", 10);
        }

        public void PrintData(string outString)
        {
            try
            {
                System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                byte[] bytes = enc.GetBytes(outString);
                buff = new MemoryStream(bytes);
                streamToPrint = new StreamReader(buff);
                PrintDocument pd = new PrintDocument();
                pd.PrinterSettings.PrinterName = Program.frmMain.strPrinterName;
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                pd.Print();
                pd.Dispose();
            }
            catch (Exception ex)
            {
                string s = "";
                Program.tLog.WriteToFile(ex.Message, ref s);
            }
            finally
            {
                if (streamToPrint != null)
                {
                    streamToPrint.Close();
                    streamToPrint.Dispose();
                }
                if (buff != null)
                {
                    buff.Close();
                    buff.Dispose();
                }              
            }
        }

        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;
            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height / this.printFont.GetHeight(ev.Graphics);
            // Print each line of the stream
            while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
            {             
                yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());                   
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;

            printFont.Dispose();
            ev.Graphics.Dispose();
        }       
    }
}



Answers (1)