Click here to Skip to main content
15,917,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I have a strange problem with the localreport.Render method. On the same data the .Render varies from 350ms to 25000ms to complete. It is also totally random. Our setup is .net framework 3.51 on winxp computers (2gh, 1gb ram). The report is a small label with maybe 10 text fields, and a bitmap for barcode. It is especially slow on first fire after program load, but is also randomly unacceptable slow during the day it is used (100-200 times in 3-5 hours period with same program session)

I have used the code from MS: http://msdn.microsoft.com/en-us/library/ms252091.aspx[^]

which i have modifed slightly to suit our needs, and i have recently added stopwatches "everywhere" in the code to isolate the trouble

My printing event:

C#
try
{
    while (antallPrint > 0)
    {
        using (print p = new print())
        {
            p.Run(AppDomain.CurrentDomain.BaseDirectory + "\\002 Til Ompakking\\rptETKOmpakkingskasse.rdlc", dtOmpakkingskasse, "dsOmpakkingskasse", printer);
        }
                        
        antallPrint--;
    }
}
catch (Exception ex)
{
    if (ex.InnerException != null)
        MessageBox.Show(ex.InnerException.Message, ex.Message);
    else
        MessageBox.Show(ex.Message, "Logidose");
}
finally
{
    this.Cursor = Cursors.Default;

}


and print code:

C#
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace LogiDose_til_ompakking._002_Til_Ompakking
{
    public class print : IDisposable
    {
        private int m_currentPageIndex;
        private IList<Stream> m_streams;
        string printerName = "Microsoft Office Document Image Writer";
        frmPrintDebug f = null;
            
       
        // Routine to provide to the report renderer, in order to
        //    save an image for each page of the report.
        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
        {
            //Stream stream = new FileStream(@"..\..\" + name + "." + fileNameExtension, FileMode.Create);

            Stream stream = new MemoryStream();
            m_streams.Add(stream);
            return stream;
        }
        // Export the given report as an EMF (Enhanced Metafile) file.
        private void Export(LocalReport report)
        {

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            sw.Start();

            /*"  <PageWidth>12cm</PageWidth>" +
              "  <PageHeight>5cm</PageHeight>" +
              "  <MarginTop>0cm</MarginTop>" +
              "  <MarginLeft>0cm</MarginLeft>" +
              "  <MarginRight>0cm</MarginRight>" +
              "  <MarginBottom>0cm</MarginBottom>" +*/

            string deviceInfo =
              "<DeviceInfo>" +
              "  <OutputFormat>EMF</OutputFormat>" +
              
              "</DeviceInfo>";
            Warning[] warnings;
            m_streams = new List<Stream>();

            report.Render("Image", deviceInfo, CreateStream, out warnings);
            
            if (f != null)
                f.textBox1.AppendText("--Report Render " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();

            foreach (Stream stream in m_streams)
                stream.Position = 0;


            if (f != null)
                f.textBox1.AppendText("--foreach stream in m_streams " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();

            if (f != null)
                foreach (Warning w in warnings)
                    f.textBox1.AppendText("--" + w.Message + "\n");
        }
        // Handler for PrintPageEvents
        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            sw.Start();


            Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);

            if (f != null)
                f.textBox1.AppendText("--Metafile " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();

            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            if (f != null)
                f.textBox1.AppendText("--Drawimage " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
            if (f != null)
                f.textBox1.AppendText("--HasMorePages " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();
        }
        
        private void Print()
        {

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            
            if (m_streams == null || m_streams.Count == 0)
                return;
            PrintDocument printDoc = new PrintDocument();
            printDoc.PrinterSettings.PrinterName = printerName;

            if (f != null)
                f.textBox1.AppendText("--Printdoc " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();
            
            if (!printDoc.PrinterSettings.IsValid)
            {
                string msg = String.Format("Can't find printer \"{0}\".", printerName);
                MessageBox.Show(msg, "Print Error");
                return;
            }

            if (f != null)
                f.textBox1.AppendText("--PrinterSettings.Isvalid " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();

            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            printDoc.Print();

            if (f != null)
                f.textBox1.AppendText("--PrintDoc.Print " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();

        }
        // Create a local report for Report.rdlc, load the data,
        //    export the report to an .emf file, and print it.
        public void Run(string reportPath, DataTable dt,string reportDatasetName, string PrinterNavn)
        {
            foreach (Form fOpen in Application.OpenForms)
            {
                if (fOpen.GetType().Equals(typeof(frmPrintDebug)))
                    f = fOpen as frmPrintDebug;
            }

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            if (f != null)
                f.textBox1.Text = "";

            sw.Start();
            printerName = PrinterNavn;
            LocalReport report = new LocalReport();
            sw.Stop();

            if(f != null)
                f.textBox1.AppendText("Localreport " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();

            report.ReportPath = reportPath;
            report.DataSources.Add(new ReportDataSource(reportDatasetName, dt));
            if (f != null)
                f.textBox1.AppendText("Datasource " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();
            if (f != null)
                f.textBox1.AppendText("Export start\n");

            Export(report);

            if (f != null)
                f.textBox1.AppendText("Export stopp " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();
            m_currentPageIndex = 0;
            if (f != null)
                f.textBox1.AppendText("Print start --\n");
            Print();
            if (f != null)
                f.textBox1.AppendText("Print stopp " + sw.ElapsedMilliseconds + "ms\n");

            sw.Reset();
            sw.Start();
        }

        public void Dispose()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }
    }
}
Posted
Updated 16-Apr-12 7:14am
v2
Comments
S@53K^S 16-Apr-12 14:29pm    
are you having problem with rendering the report or printing the data ? and could you provide some explanation as to what exactly you are trying to do ?It seems totally out of place with all the code and problem.Please provide specifics of the issue
MrDeej 16-Apr-12 15:16pm    
I am trying to print directly without user dialogs, and the microsoft preffered solution to this is the large code which i also linked to


This line takes from 250ms to 25 000ms, with exactly the same data/input. I cannot understand why:
report.Render("Image", deviceInfo, CreateStream, out warnings);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900