Click here to Skip to main content
15,893,486 members
Home / Discussions / C#
   

C#

 
QuestionCopy directories Pin
Stefan_ Spenz11-Sep-05 22:13
Stefan_ Spenz11-Sep-05 22:13 
AnswerRe: Copy directories Pin
rakesh_nits11-Sep-05 22:34
rakesh_nits11-Sep-05 22:34 
Questionmonitoring directories Pin
rakesh_nits11-Sep-05 21:51
rakesh_nits11-Sep-05 21:51 
AnswerRe: monitoring directories Pin
Dave Kreskowiak12-Sep-05 5:44
mveDave Kreskowiak12-Sep-05 5:44 
QuestionDrawing an arc Pin
11-Sep-05 21:08
suss11-Sep-05 21:08 
QuestionMail Recieve Notification Pin
mail2bourne11-Sep-05 20:34
mail2bourne11-Sep-05 20:34 
AnswerRe: Mail Recieve Notification Pin
S. Senthil Kumar11-Sep-05 21:09
S. Senthil Kumar11-Sep-05 21:09 
Questionsystem.threading.timer Pin
dhol11-Sep-05 20:02
dhol11-Sep-05 20:02 
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 description for Form1.
///

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;
///
/// Required designer variable.
///

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
//
}

///
/// Clean up any resources being used.
///

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.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

///
/// The main entry point for the application.
///

[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..


dhol
AnswerRe: system.threading.timer Pin
g00fyman11-Sep-05 20:29
g00fyman11-Sep-05 20:29 
AnswerRe: system.threading.timer Pin
S. Senthil Kumar11-Sep-05 21:07
S. Senthil Kumar11-Sep-05 21:07 
GeneralRe: system.threading.timer Pin
dhol12-Sep-05 2:25
dhol12-Sep-05 2:25 
Questiontrapping dockpadding changes Pin
microsoc11-Sep-05 19:58
microsoc11-Sep-05 19:58 
AnswerRe: trapping dockpadding changes Pin
leppie11-Sep-05 21:14
leppie11-Sep-05 21:14 
AnswerRe: trapping dockpadding changes Pin
leppie11-Sep-05 21:19
leppie11-Sep-05 21:19 
QuestionRe: trapping dockpadding changes Pin
microsoc11-Sep-05 21:39
microsoc11-Sep-05 21:39 
QuestionResizing C# windows application controllers in run time Pin
cshaaaa11-Sep-05 17:53
cshaaaa11-Sep-05 17:53 
AnswerRe: Resizing C# windows application controllers in run time Pin
Rahul Walavalkar11-Sep-05 18:13
Rahul Walavalkar11-Sep-05 18:13 
GeneralRe: Resizing C# windows application controllers in run time Pin
cshaaaa11-Sep-05 20:44
cshaaaa11-Sep-05 20:44 
QuestionUrgent question about Updates Pin
nidhelp11-Sep-05 17:50
nidhelp11-Sep-05 17:50 
AnswerRe: Urgent question about Updates Pin
Christian Graus11-Sep-05 18:49
protectorChristian Graus11-Sep-05 18:49 
GeneralRe: Urgent question about Updates Pin
nidhelp11-Sep-05 19:41
nidhelp11-Sep-05 19:41 
GeneralRe: Urgent question about Updates Pin
Christian Graus12-Sep-05 12:00
protectorChristian Graus12-Sep-05 12:00 
QuestionBroadcastEventWindow not responding and program lockup Pin
0ryan011-Sep-05 16:19
0ryan011-Sep-05 16:19 
AnswerRe: BroadcastEventWindow not responding and program lockup Pin
S. Senthil Kumar11-Sep-05 21:56
S. Senthil Kumar11-Sep-05 21:56 
AnswerRe: BroadcastEventWindow not responding and program lockup Pin
leppie11-Sep-05 22:00
leppie11-Sep-05 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.