Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is an example of BackGroundWorker usage.
- // To test it, create a Windows Forms Application project, and replace the code in Form1.cs with the following.
- // Once you run the application, click the button and the 2 datagridviews will be filled from the worker thread.
- using System;
- using System.ComponentModel;
- using System.Data;
- using System.Windows.Forms;
- using System.Threading;
- namespace Forms
- {
- public partial class Form1 : Form
- {
- #region Helper classes
- class NewDataSource
- {
- public int Grid { get; set; } // is the data source for grid 1 or 2
- public DataTable DataSource { get; set; }
- }
- class NewLabel
- {
- public int Label { get; set; } // is the text for label 1 or 2
- public string Text { get; set; }
- }
- #endregion
- #region Fields
- SplitContainer splitContainer;
- DataGridView dataGridView1;
- DataGridView dataGridView2;
- TextBox textBox1;
- TextBox textBox2;
- Button button;
- BackgroundWorker bkgWorker;
- static Random rand = new Random(123);
- #endregion
- #region CTOR
- public Form1()
- {
- InitializeComponent();
- this.InitControls();
- this.InitBackGroundWorker();
- }
- #endregion
- #region Initializations
- private void InitControls()
- {
- this.textBox1 = new TextBox();
- this.textBox1.Dock = DockStyle.Top;
- this.textBox1.ReadOnly = true;
- this.textBox1.Text = " ---- empty ---- ";
- this.textBox2 = new TextBox();
- this.textBox2.Dock = DockStyle.Top;
- this.textBox2.ReadOnly = true;
- this.textBox2.Text = " ---- empty ---- ";
- this.dataGridView1 = new DataGridView();
- this.dataGridView1.Dock = DockStyle.Fill;
- this.dataGridView2 = new DataGridView();
- this.dataGridView2.Dock = DockStyle.Fill;
- this.splitContainer = new SplitContainer();
- this.splitContainer.Dock = DockStyle.Fill;
- this.splitContainer.Panel1.Controls.Add(this.textBox1);
- this.splitContainer.Panel1.Controls.Add(this.dataGridView1);
- this.splitContainer.Panel2.Controls.Add(this.textBox2);
- this.splitContainer.Panel2.Controls.Add(this.dataGridView2);
- this.button = new Button();
- this.button.Dock = DockStyle.Bottom;
- this.button.Text = "Start Elaboration";
- this.button.Click += new EventHandler(button_Click);
- this.Controls.Add(this.splitContainer);
- this.Controls.Add(this.button);
- this.splitContainer.SplitterDistance = (this.splitContainer.Width - this.splitContainer.SplitterWidth) / 2;
- }
- private void InitBackGroundWorker()
- {
- this.bkgWorker = new BackgroundWorker();
- this.bkgWorker.WorkerReportsProgress = true;
- this.bkgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
- this.bkgWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
- this.bkgWorker.DoWork += new DoWorkEventHandler(worker_DoWork);
- }
- #endregion
- #region Event Handlers
- void button_Click(object sender, EventArgs e)
- {
- this.button.Enabled = false; // disable the button to avoid multiple elaborations
- this.bkgWorker.RunWorkerAsync();
- }
- void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- if (e.UserState is NewLabel)
- {
- var newLabel = (NewLabel)e.UserState;
- if (newLabel.Label == 0)
- this.textBox1.Text = newLabel.Text;
- else if (newLabel.Label == 1)
- this.textBox2.Text = newLabel.Text;
- }
- else if (e.UserState is NewDataSource)
- {
- var newDataSource = (NewDataSource)e.UserState;
- if (newDataSource.Grid == 0)
- this.dataGridView1.DataSource = newDataSource.DataSource;
- else if (newDataSource.Grid == 1)
- this.dataGridView2.DataSource = newDataSource.DataSource;
- }
- }
- void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- {
- this.button.Enabled = true; // enable the button again
- }
- void worker_DoWork(object sender, DoWorkEventArgs e)
- {
- // we're on another thread (not the UI thread)
- // so we can perform the elaboration here
- var worker = (BackgroundWorker)sender;
- // get data for GRID and LABEL 1
- var data1 = GetDataFromDB();
- worker.ReportProgress(0, new NewLabel() { Label = 0, Text = "Grid has: " + data1.Rows.Count + " rows" });
- Thread.Sleep(1000); // simulate another delay of 1 sec
- worker.ReportProgress(0, new NewDataSource() { Grid = 0, DataSource = data1 });
- // get data for GRID and LABEL 2
- var data2 = GetDataFromDB();
- worker.ReportProgress(0, new NewLabel() { Label = 1, Text = "Grid has: " + data2.Rows.Count + " rows" });
- Thread.Sleep(1000); // simulate another delay of 1 sec
- worker.ReportProgress(0, new NewDataSource() { Grid = 1, DataSource = data2 });
- // N.B. here we're updating the datagridview whenever we get the data.
- // another approach could be collecting all the datasources and set them to e.Result = datasources
- // then update the datagridviews after the finish of the elaboration in RunWorkerCompleted event handler
- }
- #endregion
- #region Elaboration Methods
- private DataTable GetDataFromDB()
- {
- var dt = new DataTable();
- dt.Columns.Add("ID", typeof(int));
- dt.Columns.Add("Name", typeof(string));
- int nRows = rand.Next(20, 300);
- for (int i = 0; i < nRows; i++)
- {
- int id = rand.Next(1, 100000);
- dt.Rows.Add(id, "Name of id: " + id);
- }
- Thread.Sleep(2000); // to simulate DB delay (2 seconds)
- return dt;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement