1403341 - How to export reports in multiple threads using the Crystal Reports .NET RAS SDK

SAP Knowledge Base Article - Public

1403341 - How to export reports in multiple threads using the Crystal Reports .NET RAS SDK

Symptom

How to export reports to PDF in multiple threads using the Crystal Reports .NET  RAS SDK

Reproducing the Issue

  • Crystal Reports XI Release 2 Service Pack 2 and later 
  • C#
  • Resolution

    sample code :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;

    using CrystalDecisions.ReportAppServer.ClientDoc;
    using CrystalDecisions.ReportAppServer.DataDefModel;
    using CrystalDecisions.ReportAppServer.ReportDefModel;
    using CrystalDecisions.ReportAppServer.CommonObjectModel;

    namespace CS08_Export_thread_param_ras_unmgd
    {
        public partial class Form1 : Form
        {
            private ReportClientDocument m_boReportClientDocument;
            private string _rptFile;
            private ByteArray m_ByteArray;

            private ManualResetEvent[] _reportEventsArray;
            private ManualResetEvent _actionAborted;
            private ManualResetEvent _actionFinished;

            public Form1()
            {
                _actionFinished = new ManualResetEvent(false);
                _actionAborted = new ManualResetEvent(false);
                _reportEventsArray = new ManualResetEvent[2];

                _reportEventsArray[0] = _actionAborted;
                _reportEventsArray[1] = _actionFinished;
           
                InitializeComponent();
                OpenReport();

                // if necessary set parameters
                SetReportParametersValues();

                // Export report to PDF
                Export();

            }


            //---------------------------------------------------------------------------------
            //                       OPEN THE REPORT
            //---------------------------------------------------------------------------------
            private void OpenReport()
            {

                //--------------------------------------------------------------------------------
                //make sure that you change the server setting of the RAS server to this folder
                
                _rptFile = "C:\\Reports\Report.rpt";
               
                try
                {
                    m_boReportClientDocument = new ReportClientDocumentClass();

                    var path = (object)_rptFile;
                    m_boReportClientDocument.Open(ref path, (int) CdReportClientDocumentOpenOptionsEnum.cdReportClientDocumentOpenAsReadOnly );
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR in OpenReport(): " + e.Message);
                }
            }

                    
            //---------------------------------------------------------------------------------
            //                       SETTING PARAMETERS VALUES METHODS
            //---------------------------------------------------------------------------------

            // Setting each parameter field from the report with a value.
            private void SetReportParametersValues()
            {
                m_boReportClientDocument.DataDefController.ParameterFieldController.SetCurrentValue("", "param1", "ParameterValue1");
                m_boReportClientDocument.DataDefController.ParameterFieldController.SetCurrentValue("", "param2", "ParameterValue2");
            }


            //---------------------------------------------------------------------------------
            //                     RUNNING THE REPORT Export METHODS
            //---------------------------------------------------------------------------------

            // Running the report in a different threads.
            private void Export()
            {
                var thread = new Thread(ExportToPDF);

                thread.Start();
                WaitForThread(thread);
            }

            private void ExportToPDF()
            {

                try
                {

                    // Export the report to the required format:
                    m_ByteArray = m_boReportClientDocument.PrintOutputController.Export(CrReportExportFormatEnum.crReportExportFormatPDF, 0);

                    string exportPath = "C:\\test.pdf";

                    m_ByteArray.Save(exportPath, true);

                    // Notify on finished
                    _actionFinished.Set();
                }

                catch (Exception e)
                {
                    Console.WriteLine("ERROR in ExportToFile(): " + e.Message);
                    _actionAborted.Set();
                }
            }

            private void WaitForThread(Thread thread)
            {
                // Wait for the thread to finish or for abort event
                var index = WaitHandle.WaitAny(_reportEventsArray);

                if (index == 0) //aborted
                {
                    // Stop the thread execution
                    thread.Abort();
                    throw new Exception("CrystalReport was aborted.");
                }
                _actionFinished.Reset();
            }
        }
    }

    Keywords

    How export reports multiple threads  Crystal Reports .NET  RAS SDK , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , How To

    Product

    SAP Crystal Reports XI R2