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:
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:
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;
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
private void Export(LocalReport report)
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
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");
}
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();
}
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;
}
}
}
}