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 !




Making Parameterized Threads in C#


Posted Date: 17 Mar 2007    Resource Type: Code Snippets    Category: C# Syntax

Posted By: Prajnan Das       Member Level: Gold
Rating:     Points: 10



Often when we start a thread, we want to give it some parameters usually some data it has to work on. The ThreadStart delegate doesn't take any parameters. So what we do if we would like to call methods of a class in Thread and that method has some parameters? In .NET 2.0, there is a new delegate, ParameterizedThreadStart, which takes a parameter of type object. You can create a thread using an instance of this delegate instead of just ThreadStart, and a new overload to Thread.Start allows you to specify the value to be passed to the new thread. This is simple, but only accepts a single parameter. There is another option in C# version 2.0 for this problem: anonymous methods. Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. Basically we create a ThreadStart delegate by passing the block of code as delegate parameter. So, this option can overcome the problem of multiple parameters.

This is a demo class called Logic, which have got overloaded methods which we want to start in Thread

public class Logic
{
public void Call(object param)
{
Console.WriteLine("Call made using {0}", param);
}

public void Call(object param1, object param2)
{
Console.WriteLine("Call made using {0} & {1}", param1, param2);
}
}

Solution 1; using ParameterizedThreadStart delegate of .NET 2.0:

//We can only call the version of Call of Logic; which takes a single parameter
ParameterizedThreadStart starter = new ParameterizedThreadStart(new Logic().Call);
new Thread(starter).Start("foo");

Solution 2; using Anonymous Methods of C# 2.0:

//Put the block of code to be executed as parameter to ThreadStart delegate
ThreadStart starter2 = delegate { new Logic().Call("foo", "bar"); };
new Thread(starter2).Start();






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

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: Creating and using static constructors
Previous Resource: Simulation of Dead Lock in Threads
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

email fax service

Contact Us    Privacy Policy    Terms Of Use