system.threading.timer

Sep 12 2005 3:01 AM

Hi

I have done a program using thread(multithreading)

with 3 text boxes, 3 labels and a button. such that when the button is clicked,
Label 1 should become green color after the entered time period in text box1.
Label 2 should become Yellow color after the entered time period in text box2.
Label 3 should become Blue color after the entered time period in text box3.


This I have done with three threads to start these processes. that is start the three threads in the buttonclick.


Here is the coding part

sing System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace multithreadtest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;

private Thread t1, t2, t3;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
t1 = new Thread(new ThreadStart(colourlabel1));
t2 = new Thread(new ThreadStart(colourlabel2));
t3 = new Thread(new ThreadStart(colourlabel3));

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.label1.Location = new System.Drawing.Point(88, 64);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Blue";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.label2.Location = new System.Drawing.Point(208, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "Green";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label3.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.label3.Location = new System.Drawing.Point(320, 64);
this.label3.Name = "label3";
this.label3.TabIndex = 2;
this.label3.Text = "Red";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(208, 24);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(320, 24);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 5;
this.textBox3.Text = "";
//
// button1
//
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button1.Location = new System.Drawing.Point(456, 40);
this.button1.Name = "button1";
this.button1.TabIndex = 6;
this.button1.Text = "Iterate";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 397);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void colourlabel1()
{
Thread.Sleep(System.Convert.ToInt32(textBox1.Text));
label1.BackColor=Color.Blue ;
}

private void colourlabel2()
{
Thread.Sleep(System.Convert.ToInt32(textBox2.Text));
label2.BackColor=Color.Green ;
}


private void colourlabel3()
{
Thread.Sleep(System.Convert.ToInt32(textBox3.Text));
label3.BackColor=Color.Red ;
}
private void button1_Click(object sender, System.EventArgs e)
{
this.button1.Enabled = false;
t1.Start();
t2.Start();
t3.Start();
}


}
}


Now I am learning system.Threading.timer class, I couldnt understand how to work with this. Can anyone please explain me how to work withthis class and can anyone add this class to the above progarm and explain me..


js