BackgroundWorker and Crystal Reports

I have a DBClass that is responsible for executing database queries and SPs.
 
Here is the code snippet that shows how to use a background thread to get data and generate reports. This is useful when the main thread of your applications is doing something else, data entry and you want reports to be generated in the background.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Threading;  
  10. namespace myCryst  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         DataSet ds;  
  15.         frmCryst f = new frmCryst();  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.         private void bgWorker_DoWork(object sender, DoWorkEventArgs e)  
  21.         {  
  22.             bgWorker.ReportProgress(10);  
  23.             Thread.Sleep(164);  
  24.             bgWorker.ReportProgress(20);  
  25.             Thread.Sleep(164);  
  26.             bgWorker.ReportProgress(30);  
  27.             Thread.Sleep(164);  
  28.             ds = DBClass.ExecQuery("select * from students");  
  29.             bgWorker.ReportProgress(40);  
  30.             Thread.Sleep(164);  
  31.             bgWorker.ReportProgress(50);  
  32.             Thread.Sleep(164);  
  33.             bgWorker.ReportProgress(70);  
  34.             Thread.Sleep(164);  
  35.             bgWorker.ReportProgress(95);  
  36.             Thread.Sleep(164);  
  37.             bgWorker.ReportProgress(100);  
  38.         }  
  39.         private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)  
  40.         {  
  41.             this.progressBar1.Value = e.ProgressPercentage;  
  42.         }  
  43.         private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
  44.         {  
  45.             showCrystalReport();  
  46.         }  
  47.         private void button1_Click(object sender, EventArgs e)  
  48.         {  
  49.             bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);  
  50.             bgWorker.WorkerReportsProgress = true;  
  51.             bgWorker.RunWorkerAsync();  
  52.         }  
  53.         private void showCrystalReport()  
  54.         {  
  55.             CrystalReport1 c = new CrystalReport1();  
  56.             Reportds d = new Reportds();  
  57.             d.Tables[0].Merge(ds.Tables["tab"]);  
  58.             frmCryst f = new frmCryst();  
  59.             c.SetDataSource(d);  
  60.             f.crystRep.ReportSource = c;  
  61.             f.Show();  
  62.         }  
  63.         private void button2_Click(object sender, EventArgs e)  
  64.         {  
  65.             Application.Exit();  
  66.         }  
  67.     }  
  68. } 


Similar Articles