1
Vote

Use of multi-core capabilities in report generation

description

CloneVisualBlock seems to work very slow. This method is called each time a header or footer section is to be cloned. So this could be done using a ThreadPool to improve performace on multi-core systems.

comments

jogibear9988 wrote Dec 13, 2012 at 9:40 AM

You can render a Chart in Background Thread ...

I've done it like this:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Windows.Xps.Packaging;

using CodeReason.Reports;
using System.Threading;


namespace ControlPanel.Views.Reports
{
/// <summary>
/// Interaction logic for ReportsViewer.xaml
/// </summary>
public partial class ReportsViewer : UserControl
{
    private string reportXaml;

    private bool firstLoad = true;

    public ReportsViewer(string ReportXaml)
    {
        InitializeComponent();

        this.reportXaml = ReportXaml;

        CreateReport();
    }

    private void CreateReport()
    {
         if (!firstLoad) return;
        firstLoad = false;

        var action = new Action(delegate
            {
                try
                {
                    ReportDocument reportDocument = new ReportDocument();

                    reportDocument.XamlData = reportXaml;
                    //reportDocument.XamlImagePath = Path.Combine(Environment.CurrentDirectory, @"Templates\");

                    ReportData data = new ReportData();

                    // set constant document values
                    data.ReportDocumentValues.Add("PrintDate", DateTime.Now); // print date is now

                    // sample table "Ean"
                    DataTable table = new DataTable("TableEan");
                    table.Columns.Add("Position", typeof(string));
                    table.Columns.Add("Item", typeof(string));
                    table.Columns.Add("EAN", typeof(string));
                    table.Columns.Add("Count", typeof(int));
                    Random rnd = new Random(1234);
                    for (int i = 1; i <= 100; i++)
                    {
                        // randomly create some items
                        StringBuilder sb = new StringBuilder();
                        for (int j = 1; j <= 13; j++) sb.Append(rnd.Next(10));
                        table.Rows.Add(new object[] { i, "Item " + i.ToString("0000"), sb.ToString(), rnd.Next(9) + 1 });
                    }
                    data.DataTables.Add(table);

                    // sample table "Example1"
                    table = new DataTable("Example1");
                    table.Columns.Add("Year", typeof(string));
                    table.Columns.Add("Value", typeof(decimal));
                    rnd = new Random(1234);
                    for (int i = 1990; i <= 2009; i++)
                    {
                        // randomly create some values
                        table.Rows.Add(new object[] { i, rnd.Next(100000) + 10000 });
                    }
                    data.DataTables.Add(table);

                    // sample table "Example2"
                    table = new DataTable("Example2");
                    table.Columns.Add("Year", typeof(string));
                    table.Columns.Add("Value", typeof(decimal));
                    int citizens = rnd.Next(100000);
                    for (int i = 1990; i <= 2009; i++)
                    {
                        // randomly create some values
                        table.Rows.Add(new object[] { i, citizens });
                        citizens += rnd.Next(10000);
                    }
                    data.DataTables.Add(table);

                    // sample table "Example3"
                    table = new DataTable("Example3");
                    table.Columns.Add("Opinion", typeof(string));
                    table.Columns.Add("Percent", typeof(decimal));
                    table.Rows.Add(new object[] { "Yes", 36.2 });
                    table.Rows.Add(new object[] { "No", 21.5 });
                    table.Rows.Add(new object[] { "Unsure", 100 - 36.2 - 21.5 });
                    data.DataTables.Add(table);

                    XpsDocument xps = reportDocument.CreateXpsDocument(data);
                    Dispatcher.BeginInvoke(new Action(() => { documentViewer.Document = xps.GetFixedDocumentSequence(); }));
                }
                catch (Exception ex)
                {
                    // show exception
                    MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
                }
                finally
                {
                    Dispatcher.BeginInvoke(new Action(() => { busyDecorator.IsBusyIndicatorHidden = true; }));
                }
            });

        System.Threading.Thread t = new System.Threading.Thread(() => action.Invoke());
        t.SetApartmentState(System.Threading.ApartmentState.STA);
        t.IsBackground = true;
        t.Start();            
    }        
}
}

wrote Feb 21, 2013 at 10:38 PM