Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 6th, 2005, 02:48 AM
Authorized User
 
Join Date: Dec 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multithreading in c# windows application

Hi
I have to work on multithreading concepts in c# windows application.

Design a form with 3 labels and 3 text boxes and a button.

I should enter the time in three text boxes. Three different times. And press the Button.
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.

I should have three threads to start three processes. Start the three threads in the button click.

Thread 1 to start the thread and wait for the entered time in text box 1 and make the label 1 green. And so on.
U should enter the time in milli seconds. 1 second = 1000 milliseconds.
So if I want to enter 5 seconds. I should enter 5000 in the text box. I should Write a windows application for this.

Pls anyone explain me how to do this . pls help me to do this sample.

js


__________________
SureShot
 
Old September 6th, 2005, 07:36 PM
Registered User
 
Join Date: Sep 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is how to do it. Just copy it, paste it, compile it, and run it.
Please let me know if this has been of some help to you.:D

Code:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace LabelColors
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.TextBox textBox2;

        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }

        private void StartThreads() {
            try {
                Thread t1 = new Thread(new ThreadStart(this.SleepThenChangeColor1));
                t1.Start();
                Thread t2 = new Thread(new ThreadStart(this.SleepThenChangeColor2));
                t2.Start();
                Thread t3 = new Thread(new ThreadStart(this.SleepThenChangeColor3));
                t3.Start();
            }
            catch (Exception e) {
                    MessageBox.Show(e.ToString(), "Exception Caught");
            }
        }
        private void SleepThenChangeColor1() {
            Thread.Sleep(Convert.ToInt32(textBox1.Text));
            label1.ForeColor = Color.Green;
        }
        private void SleepThenChangeColor2() {    
            Thread.Sleep(Convert.ToInt32(textBox2.Text));
            label2.ForeColor = Color.Yellow;
        }
        private void SleepThenChangeColor3() {    
            Thread.Sleep(Convert.ToInt32(textBox3.Text));
            label3.ForeColor = Color.Blue;
        }
        [STAThread]
        public static void Main(string[] args)
        {
            Application.Run(new MainForm());
        }

        #region Windows Forms Designer generated code
        /// <summary>
        /// This method is required for Windows Forms designer support.
        /// Do not change the method contents inside the source code editor. The Forms designer might
        /// not be able to load this method if it was changed manually.
        /// </summary>
        private void InitializeComponent() {
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(124, 40);
            this.textBox2.Name = "textBox2";
            this.textBox2.TabIndex = 4;
            this.textBox2.Text = "2000";
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(228, 40);
            this.textBox3.Name = "textBox3";
            this.textBox3.TabIndex = 5;
            this.textBox3.Text = "3000";
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("Tahoma", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.button1.Location = new System.Drawing.Point(22, 64);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(306, 23);
            this.button1.TabIndex = 6;
            this.button1.Text = "Start Threads";
            this.button1.Click += new System.EventHandler(this.Button1Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(20, 40);
            this.textBox1.Name = "textBox1";
            this.textBox1.TabIndex = 3;
            this.textBox1.Text = "1000";
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label1.Location = new System.Drawing.Point(20, 14);
            this.label1.Name = "label1";
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label2.Location = new System.Drawing.Point(124, 14);
            this.label2.Name = "label2";
            this.label2.TabIndex = 1;
            this.label2.Text = "label2";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label3.Location = new System.Drawing.Point(228, 14);
            this.label3.Name = "label3";
            this.label3.TabIndex = 2;
            this.label3.Text = "label3";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(348, 100);
            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 = "MainForm";
            this.Text = "Changing Labels";
            this.ResumeLayout(false);
        }
        #endregion
    //    void TextBox3TextChanged(object sender, System.EventArgs e)
    //    {

    //    }

    void Button1Click(object sender, System.EventArgs e)
    {
        StartThreads();
    }

    }
}
 
Old September 10th, 2005, 02:04 AM
Authorized User
 
Join Date: Dec 2004
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
I did that already,but anyway very much thanks fo rur help .

Now i want to develop this progarm with the help of system.threading.timer class
i mean using system.threading.timer class, canu please do soemthing to this progarm and explain wht it does.
i wantto knbow abt this class.
and it advantages.can u please explainit .

dhol






Similar Threads
Thread Thread Starter Forum Replies Last Post
Application is Web Or Windows! How to Identify? sizzlin_samy General .NET 1 October 8th, 2007 11:20 AM
Need help with windows application and DLL naikpalak722 General .NET 0 October 2nd, 2007 03:08 PM
Web Application OR Windows Application adesilva .NET Framework 2.0 2 May 4th, 2007 07:12 AM
Windows Form Application help tycotrix C# 4 January 11th, 2007 07:15 AM
Web application Vs Windows Application Ned .NET Web Services 2 January 20th, 2004 01:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.