Team Foundation Server Hosting
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » .NET General » Uisng threads in Report generation
       
Author Reply
HERBERTS NGOBOLA
posted 17 posts
since Apr 22, 2010 
from U.K

Uisng threads in Report generation

  Posted on: 20 Jul 2010       

Sometimes we need create many reports in multiple threads simultaneously. This may be the development of a web service, or output information from an existing multithreaded application in a certain needed document format (PDF as example).

I noted that FastReport VCL library is a better solution for document generation in multiple formats. This component library is easy to use and has a convenient report designer which allows you to easily connect to different data sources, among which may be internal application data - arrays, sets of parameters, etc.

Traditional use of FastReport does not give any difficulties, but now we have to use this report generator in a multithreaded application. The output file format will be PDF.

TfrxReport class has a description of several properties which need to be set immediately after the creation of the object. 

You need to remember that the object must work in a thread without the creation of dialogs, progress bars and other visual information.

Here is an example of creating and setting an object of class TfrxReport before the execution of the report:

// create report

FReport := TfrxReport.Create(nil);

// disable all messages

FReport.EngineOptions.SilentMode := True;

// enable safe work in threads

FReport.EngineOptions.EnableThreadSafe := True;

// disable cache

FReport.EngineOptions.UseFileCache := false;

// disable progress bar

FReport.ShowProgress := False;

 

Some reports have integrated dialog forms, and showing them should be banned for obvious reasons.  We need to override an event handler TfrxReport.Engine.OnRunDialog by the procedure ShowReportDialog for to any dialogs.

// handle all dialogs by ShowReportDialog

FReport.Engine.OnRunDialog := ShowReportDialog;

 

Our procedure will be executed instead of showing each report dialog. We can change the state of any control in a dialog, but we will leave this procedure empty.

procedure TTestThread.ShowReportDialog(Page: TfrxDialogPage);

begin

  // empty

end;

 

Then we create an object of TfrxPDFExport and disable the showing of dialog window and progress bar.

  PDF := TfrxPDFExport.Create(nil);

  PDF.ShowDialog := False;

  PDF.ShowProgress := False;

 

All operations on the creation and exporting of report objects can be done in the constructor of the thread. The destructor of the thread should look like:

destructor TTestThread.Destroy;

begin

  // destroy all created objects

  PDF.Free;

  FReport.Free;

  inherited;

end;

 

Necessary objects are created and configured. Now you can load the report template from a file and run a report in the implementation of the main thread procedure Execute. There, it will also be exported to the desired format.

 

// load report template

FReport.LoadFromFile(FFileName);

// set report variables

FReport.Variables['ThreadID'] := QuotedStr(FId);

// run report

if FReport.PrepareReport then

begin

  // save result in PDF

  PDF.FileName := FOutPath + '\report_'+ FId +
     '_' + FormatDateTime('YYYYYMMDDHHMMSS', Now) + '.pdf';

  FReport.Export(PDF);

end;

 

Try building reports without using RichText objects because by so doing you can get an unstable application.

 

Do not forget to include ActiveX controls in uses module, and add a call to CoInitialize (nil); in the procedure Execute before creating a report if the report is connected to ADO. Call to CoUninitialize at the end of the thread procedure.

 

// thread function

procedure TTestThread.Execute;

begin

    // initialize COM library in current thread

    CoInitialize(nil);

    try

      // load report template from the file

      FReport.LoadFromFile(FFileName);

      ...

      ...

      ...

    finally

      // Uninitialize COM

      CoUninitialize;

    end;

  end;

end;

 

You can see an attached example with the application which creates 10 reports in multiple threads and write many PDF files.

Perfecting the art of reporting
       
Team Foundation Server Hosting
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved