none
LocalReport Export/Print inside BackgroundWorker or another Thread RRS feed

  • Question

  • Hi everyone!

    I need to run some invoice generation reports inside a backgroundworker so I can report that n o n invoices were printed.

    As a quick info here's the logic Im using, Process is being called from DoWork() on BackgroundWorker or from Thread:

            public void Process()
            {
                List<Invoice> data = dataFactory.GetInvoicesToPrint();
                foreach(Invoice invoice in data)
                {
                    this.PrintInvoice(invoice);
                }
            }
    
            private void PrintInvoice(Invoice invoice)
            {
                LocalReport report = new LocalReport();
                report.ReportPath = "Invoice.rdlc";
                report.EnableExternalImages = true;
                ReportParameter p = new ReportParameter("ReportLogo", logoPath);
                report.SetParameters(new ReportParameter[] { p });
                report.DataSources.Add(new ReportDataSource("Invoice", invoice));
                report.SubreportProcessing += new SubreportProcessingEventHandler(SubReportHandler);
                Export(report);
                m_currentPageIndex = 0;
                Print();
                report.SubreportProcessing -= new SubreportProcessingEventHandler(SubReportHandler);
                report.DataSources.Clear();
                report.Dispose();
                report = null;
            }
    
            private IList<Stream> m_streams;
            private int m_currentPageIndex;
    
            private void Export(LocalReport report)
            {
                string deviceInfo =
                  "<DeviceInfo>" +
                  "  <OutputFormat>EMF</OutputFormat>" +
                  "  <PageWidth>8.5in</PageWidth>" +
                  "  <PageHeight>11in</PageHeight>" +
                  "  <MarginTop>0.25in</MarginTop>" +
                  "  <MarginLeft>0.25in</MarginLeft>" +
                  "  <MarginRight>0.25in</MarginRight>" +
                  "  <MarginBottom>0.25in</MarginBottom>" +
                  "</DeviceInfo>";
                Warning[] warnings;
                m_streams = new List<Stream>();
                report.Render("Image", deviceInfo, CreateStream, out warnings);
                foreach (Stream stream in m_streams)
                    stream.Position = 0;
            }
    
            private Stream CreateStream()
            {
                Stream stream = new MemoryStream();
                m_streams.Add(stream);
                return stream;
            }
    
            private void Print(string printName, int operacion)
            {
    
                if (m_streams == null || m_streams.Count == 0)
                    return;
                PrintDocument printDoc = new PrintDocument();
    
                printDoc.PrintController = new StandardPrintController();
    
                printDoc.PrinterSettings.PrinterName = printName;
                if (!printDoc.PrinterSettings.IsValid)
                {
                    return;
                }
                printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                printDoc.Print();
            }
    
            private void PrintPage(object sender, PrintPageEventArgs ev)
            {
                Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
                ev.Graphics.DrawImage(pageImage, ev.PageBounds);
                m_currentPageIndex++;
                ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
            }
    

    Im getting some errors sometimes? Do you have any advice or tip on this?

    Thursday, April 8, 2010 8:44 PM

All replies