
|
Visual C# Windows Form Asynchronous Example
|
|---|
|
Visual C# Windows Form Asynchronous Example
Visual C# Windows Form Asynchronous Example
Category: .Net Framework, Visual C#, Windows Forms, Asynchronous Methods,
Posted 12/13/2002,
By Mike G.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace AsynchronousExample
{
public class frmAsynchronous : System.Windows.Forms.Form
{
///
/// First asynchronous delegate for DataSet #1 example
///
private delegate void FillDataSet1();
// this is the variable we use to call BeginInvoke and EndInvoke
private FillDataSet1 fds1;
///
/// Second asynchronous delegate for DataSet #2 example
///
private delegate void FillDataSet2();
// this is the variable we use to call BeginInvoke and EndInvoke
private FillDataSet2 fds2;
private String s1, s2;
private DataSet ds1 = new DataSet();
private DataSet ds2 = new DataSet();
///
/// Delegate for updating DataSource on the DataGrid control.
///
private delegate void UpdateDataGridDelegate();
private System.Windows.Forms.GroupBox gbAsynchronous;
private System.Windows.Forms.GroupBox gbDataSet1;
private System.Windows.Forms.GroupBox gbDataSet2;
private System.Windows.Forms.Button btnDataSet1;
private System.Windows.Forms.Button btnDataSet2;
private System.Windows.Forms.DataGrid dg1;
private System.Windows.Forms.DataGrid dg2;
private System.Windows.Forms.Button btnClear1;
private System.Windows.Forms.Button btnClear2;
private System.ComponentModel.Container components = null;
public frmAsynchronous()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.gbAsynchronous = new System.Windows.Forms.GroupBox();
this.gbDataSet2 = new System.Windows.Forms.GroupBox();
this.btnClear2 = new System.Windows.Forms.Button();
this.dg2 = new System.Windows.Forms.DataGrid();
this.btnDataSet2 = new System.Windows.Forms.Button();
this.gbDataSet1 = new System.Windows.Forms.GroupBox();
this.btnClear1 = new System.Windows.Forms.Button();
this.dg1 = new System.Windows.Forms.DataGrid();
this.btnDataSet1 = new System.Windows.Forms.Button();
this.gbAsynchronous.SuspendLayout();
this.gbDataSet2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dg2)).BeginInit();
this.gbDataSet1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dg1)).BeginInit();
this.SuspendLayout();
//
// gbAsynchronous
//
this.gbAsynchronous.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.gbAsynchronous.Controls.AddRange(new System.Windows.Forms.Control[] {
this.gbDataSet2,
this.gbDataSet1});
this.gbAsynchronous.Location = new System.Drawing.Point(8, 8);
this.gbAsynchronous.Name = "gbAsynchronous";
this.gbAsynchronous.Size = new System.Drawing.Size(432, 360);
this.gbAsynchronous.TabIndex = 0;
this.gbAsynchronous.TabStop = false;
this.gbAsynchronous.Text = "MTG\'s C# Asynchronous Example";
//
// gbDataSet2
//
this.gbDataSet2.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.gbDataSet2.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnClear2,
this.dg2,
this.btnDataSet2});
this.gbDataSet2.Location = new System.Drawing.Point(8, 184);
this.gbDataSet2.Name = "gbDataSet2";
this.gbDataSet2.Size = new System.Drawing.Size(416, 168);
this.gbDataSet2.TabIndex = 1;
this.gbDataSet2.TabStop = false;
this.gbDataSet2.Text = "DataSet #2";
//
// btnClear2
//
this.btnClear2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnClear2.Location = new System.Drawing.Point(328, 144);
this.btnClear2.Name = "btnClear2";
this.btnClear2.Size = new System.Drawing.Size(75, 16);
this.btnClear2.TabIndex = 3;
this.btnClear2.Text = "clear";
this.btnClear2.Click += new System.EventHandler(this.btnClear2_Click);
//
// dg2
//
this.dg2.CaptionBackColor = System.Drawing.SystemColors.ControlDark;
this.dg2.CaptionText = "DataSet Table #2";
this.dg2.CaptionVisible = false;
this.dg2.DataMember = "";
this.dg2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dg2.Location = new System.Drawing.Point(8, 56);
this.dg2.Name = "dg2";
this.dg2.Size = new System.Drawing.Size(400, 88);
this.dg2.TabIndex = 2;
//
// btnDataSet2
//
this.btnDataSet2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnDataSet2.Location = new System.Drawing.Point(8, 16);
this.btnDataSet2.Name = "btnDataSet2";
this.btnDataSet2.Size = new System.Drawing.Size(400, 32);
this.btnDataSet2.TabIndex = 1;
this.btnDataSet2.Text = "Fill DataSet #2 with 100 Rows then Wait a While";
this.btnDataSet2.Click += new System.EventHandler(this.btnDataSet2_Click);
//
// gbDataSet1
//
this.gbDataSet1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.gbDataSet1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnClear1,
this.dg1,
this.btnDataSet1});
this.gbDataSet1.Location = new System.Drawing.Point(8, 16);
this.gbDataSet1.Name = "gbDataSet1";
this.gbDataSet1.Size = new System.Drawing.Size(416, 160);
this.gbDataSet1.TabIndex = 0;
this.gbDataSet1.TabStop = false;
this.gbDataSet1.Text = "DataSet #1";
//
// btnClear1
//
this.btnClear1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnClear1.Location = new System.Drawing.Point(328, 136);
this.btnClear1.Name = "btnClear1";
this.btnClear1.Size = new System.Drawing.Size(75, 16);
this.btnClear1.TabIndex = 2;
this.btnClear1.Text = "clear";
this.btnClear1.Click += new System.EventHandler(this.btnClear1_Click);
//
// dg1
//
this.dg1.CaptionBackColor = System.Drawing.SystemColors.ControlDark;
this.dg1.CaptionText = "DataSet Table #1";
this.dg1.CaptionVisible = false;
this.dg1.DataMember = "";
this.dg1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dg1.Location = new System.Drawing.Point(8, 56);
this.dg1.Name = "dg1";
this.dg1.Size = new System.Drawing.Size(400, 80);
this.dg1.TabIndex = 1;
//
// btnDataSet1
//
this.btnDataSet1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnDataSet1.Location = new System.Drawing.Point(8, 16);
this.btnDataSet1.Name = "btnDataSet1";
this.btnDataSet1.Size = new System.Drawing.Size(400, 32);
this.btnDataSet1.TabIndex = 0;
this.btnDataSet1.Text = "Fill DataSet #1 with 100 Rows then Wait a While";
this.btnDataSet1.Click += new System.EventHandler(this.btnDataSet1_Click);
//
// frmAsynchronous
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(448, 374);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.gbAsynchronous});
this.Name = "frmAsynchronous";
this.Text = "MTG\'s C# Asynchronous Example";
this.Load += new System.EventHandler(this.frmAsynchronous_Load);
this.gbAsynchronous.ResumeLayout(false);
this.gbDataSet2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dg2)).EndInit();
this.gbDataSet1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dg1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new frmAsynchronous());
}
private void btnDataSet1_Click(object sender, System.EventArgs e)
{
try
{
s1 = btnDataSet1.Text;
btnDataSet1.Text = "Waiting for DataSet to be filled...";
btnDataSet1.Enabled = false;
AsyncCallback cb = new AsyncCallback(AsyncCallbackTarget1);
fds1 = new FillDataSet1(DelegateTarget1);
IAsyncResult ar = fds1.BeginInvoke(cb, null);
}
catch (Exception err)
{
MessageBox.Show(err.Message + "\r\n" + err.StackTrace);
}
}
private void DelegateTarget1()
{
try
{
// lets lock the DataSet until finished:
lock (ds1)
{
if (ds1.Tables.Count > 0) ds1.Tables.RemoveAt(0);
DataTable tbl = ds1.Tables.Add("TempTable");
tbl.Columns.Add("TempColumn");
for (int i = 1; i <= 100; i++)
{
DataRow r = tbl.NewRow();
r["TempColumn"] = "X";
tbl.Rows.Add(r);
}
Monitor.Wait(ds1, new TimeSpan(0, 0, 0, 10));
}
// We cannot access the DataGrid control directly
// because it was created on a different thread and
// doing so would create an exception; therefore, we
// must use a delegate that updates the control so
// so that it is updated on the same thread the control
// was created on; this only pertains to complex controls
// such as the DataGrid:
UpdateDataGridDelegate dlg =
new UpdateDataGridDelegate(UpdateDataGridTarget1);
dg1.Invoke(dlg);
}
catch (Exception err)
{
MessageBox.Show(err.Message + "\r\n" + err.StackTrace);
}
}
private void UpdateDataGridTarget1()
{
dg1.SetDataBinding(ds1, "TempTable");
}
private void AsyncCallbackTarget1(IAsyncResult ar)
{
try
{
// we must end the call with EndInvoke()
fds1.EndInvoke(ar);
btnDataSet1.Text = s1;
btnDataSet1.Enabled = true;
}
catch (Exception err)
{
MessageBox.Show(err.Message + "\r\n" + err.StackTrace);
}
}
private void btnClear1_Click(object sender, System.EventArgs e)
{
dg1.DataSource = null;
}
private void btnClear2_Click(object sender, System.EventArgs e)
{
dg2.DataSource = null;
}
private void frmAsynchronous_Load(object sender, System.EventArgs e)
{
//
}
private void btnDataSet2_Click(object sender, System.EventArgs e)
{
try
{
s2 = btnDataSet1.Text;
btnDataSet2.Text = "Waiting for DataSet to be filled...";
btnDataSet2.Enabled = false;
AsyncCallback cb = new AsyncCallback(AsyncCallbackTarget2);
fds2 = new FillDataSet2(DelegateTarget2);
IAsyncResult ar = fds2.BeginInvoke(cb, null);
}
catch (Exception err)
{
MessageBox.Show(err.Message + "\r\n" + err.StackTrace);
}
}
private void DelegateTarget2()
{
try
{
// lets lock the DataSet until finished:
lock (ds2)
{
if (ds2.Tables.Count > 0) ds2.Tables.RemoveAt(0);
DataTable tbl = ds2.Tables.Add("TempTable");
tbl.Columns.Add("TempColumn");
for (int i = 1; i <= 100; i++)
{
DataRow r = tbl.NewRow();
r["TempColumn"] = "X";
tbl.Rows.Add(r);
}
Monitor.Wait(ds2, new TimeSpan(0, 0, 0, 10));
}
// We cannot access the DataGrid control directly
// because it was created on a different thread and
// doing so would create an exception; therefore, we
// must use a delegate that updates the control so
// so that it is updated on the same thread the control
// was created on; this only pertains to complex controls
// such as the DataGrid:
UpdateDataGridDelegate dlg =
new UpdateDataGridDelegate(UpdateDataGridTarget2);
dg2.Invoke(dlg);
}
catch (Exception err)
{
MessageBox.Show(err.Message + "\r\n" + err.StackTrace);
}
}
private void UpdateDataGridTarget2()
{
dg2.SetDataBinding(ds2, "TempTable");
}
private void AsyncCallbackTarget2(IAsyncResult ar)
{
try
{
// we must end the call with EndInvoke()
fds2.EndInvoke(ar);
btnDataSet2.Text = s2;
btnDataSet2.Enabled = true;
}
catch (Exception err)
{
MessageBox.Show(err.Message + "\r\n" + err.StackTrace);
}
}
}
}
|
|