Aug 22, 2011

A Simple Thread example in Visual C#.Net 2005

This article explains how to use the thread and how to display the time in visual studio c#.net 2005.


What is thread? 

Threading means to handle some method repeatedly without interrupting other processes. I will explain a thread with a simple example. Consider that, we are going to display the digital clock with the date in the Form.
To do the thread we need the ‘Threading’ namespaces,
Import it as,





using System.Threading;
and also, we have to create 2 buttons for start the thread and stop the thread. One label for display the current date and time.

To initialise the thread, create a Thread object under the class as a global like this,



Thread th;


On the first button click event write down the following code,





th = new Thread(new ThreadStart(disp));
th.IsBackground = true;
th.Start();
[/cod]

This means, to start a ‘disp’ method we initialise the Threadstart object.
‘th.IsBackground = true’ means the thread run in the background. That means, the systems takes it as the task was finished. 
To invoke the thread use, ‘th.Start()’ method.

Then we have to ‘disp()’ method, which is the repeated task.



void disp()
{
            while (true)
            {
                label1.Text = DateTime.Now().ToString();
                Thread.Sleep(1000);
            }
}





The Above method will fine work in Visual C# 2002. but in 2005 it will not work. Because, in 2002 version the C# will accept the unsafe thread. But 2005 will accept the safe thread only. 
So we are going to rewrite the above code as,



Here we have to Invoke the lebel1. To do this we have to use delegate concept. So add the delegate as global.


private delegate void TickerDelegate(string s);
TickerDelegate tickerDelegate1;
In the constructor instantiate the delagate as


tickerDelegate1 = new TickerDelegate(SetLeftTicker);

SetLeftTicker’ is a method passed as a argument to the delegate.
So again we have to define that method.

private void SetLeftTicker(string s)
{
  label1.Text = s ;
}


Hereafter the clock will run repeatedly in the 1000 milliseconds gap.

To pause the clock, use the next button. On the button click event, write the code as,


If(th.IsAlive)
{
th.Suspend();
}




Here I am giving you the detailed Code as,


#region Using directives

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

#endregion

namespace Clock
{
    partial class Form1 : Form
    {
        Thread th1;
        private delegate void TickerDelegate(string s);
        TickerDelegate tickerDelegate1;
        public Form1()
        {
            InitializeComponent();
            tickerDelegate1 = new TickerDelegate(SetLeftTicker);
            
        }

        public void button1_Click(object sender, EventArgs e)
        {
            
            th1 = new Thread(new ThreadStart(disp));
            th1.IsBackground = true;
            th1.Start();
        }

        private void SetLeftTicker(string s)
        {
            label1.Text = s ;
        }

        void disp()
        {
            while (true)
            {
                label1.Invoke(tickerDelegate1, new object[] { DateTime.Now.ToString() });
                Thread.Sleep(1000);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (th1.IsAlive)
            {
                th1.Suspend();
            }
        }

       
    }
}

















No comments:

Post a Comment