C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




A Simple Thread example in Visual C#.Net 2005


Posted Date: 25 Oct 2004    Resource Type: Articles    Category: .NET Framework

Posted By: K.Senthil Kumar       Member Level: Bronze
Rating:     Points: 15



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,


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


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();
}
}


}
}



Note that, Classes are partial classes in vs.net 2005.

If u have any doubt regarding this article, feel free to contact sensoft2000@yahoo.com.




Responses

Author: tvkrao    06 Dec 2004Member Level: Gold   Points : 0
Hi, I like the artical about the comparision of .NET 2002 to 2005 the thread difference.

But the definition what you have written:
What is thread?
That means to handle some method repeatedly without interrupt other process

Comment: According to me Every process is having default one thread that is called main thread.

With in the main thread, we can create multiple thread to run concurrently but that should know the process.

Thread is nothing but path of execution within the process. In that main process or main thread should share the schedule the task based on thread priority when we are implementing on that time.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Threading in C#  .  Threading example in C#  .  Simple Threading examples in C#  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Extended Textbox
Previous Resource: Notification Services (.NET and MS SQL Server)
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

conference calls

Contact Us    Privacy Policy    Terms Of Use