Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / C#

Threading and UI

Rate me:
Please Sign up or sign in to vote.
4.50/5 (14 votes)
12 Dec 20042 min read 73.5K   1.3K   36  
A simple asynchronous threading example for keeping the UI code relatively free from worker thread delegate proxy declarations.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace AsynchTask
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmCalculations : System.Windows.Forms.Form
	{
  Task Task1;
  private System.Windows.Forms.Label lblRunLoops;
  private System.Windows.Forms.Button btnRunLoops;
  private System.Windows.Forms.TextBox txtValue;
  private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmCalculations()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

   Task1 = new Task();
   Task1.LoopComplete += new Task.LoopCompleteHandler(this.LoopDoneHandler);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
   this.lblRunLoops = new System.Windows.Forms.Label();
   this.btnRunLoops = new System.Windows.Forms.Button();
   this.txtValue = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   // 
   // lblRunLoops
   // 
   this.lblRunLoops.Location = new System.Drawing.Point(128, 64);
   this.lblRunLoops.Name = "lblRunLoops";
   this.lblRunLoops.Size = new System.Drawing.Size(104, 23);
   this.lblRunLoops.TabIndex = 3;
   // 
   // btnRunLoops
   // 
   this.btnRunLoops.Location = new System.Drawing.Point(16, 64);
   this.btnRunLoops.Name = "btnRunLoops";
   this.btnRunLoops.Size = new System.Drawing.Size(104, 23);
   this.btnRunLoops.TabIndex = 8;
   this.btnRunLoops.Text = "Run a Loop";
   this.btnRunLoops.Click += new System.EventHandler(this.btnRunLoops_Click);
   // 
   // txtValue
   // 
   this.txtValue.Location = new System.Drawing.Point(16, 40);
   this.txtValue.Name = "txtValue";
   this.txtValue.TabIndex = 9;
   this.txtValue.Text = "";
   // 
   // label1
   // 
   this.label1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
   this.label1.ForeColor = System.Drawing.Color.MediumBlue;
   this.label1.Location = new System.Drawing.Point(16, 16);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(280, 16);
   this.label1.TabIndex = 10;
   this.label1.Text = "Enter the number of loops to run in the text box below.";
   // 
   // frmCalculations
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(336, 101);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.txtValue);
   this.Controls.Add(this.btnRunLoops);
   this.Controls.Add(this.lblRunLoops);
   this.Name = "frmCalculations";
   this.Text = "frmCalculations";
   this.ResumeLayout(false);

  }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmCalculations());
		}

  private void btnRunLoops_Click(object sender, System.EventArgs e)
  {
   Task1.varLoopValue = int.Parse(txtValue.Text);
   btnRunLoops.Enabled = false;
   lblRunLoops.Text = "Looping...";

   Task1.StartTask();
  }

  protected void LoopDoneHandler(double Calculations, int Count)
  {
   lblRunLoops.Text = Count.ToString();
   btnRunLoops.Enabled = true;
  }
 
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions