dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersnaveensanagasetti
Ultimaterengan
parthiban
sivakrishna
Rakesh Chaubey
Jeeva
Sundar
Ram
Jivani
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » C# Syntax

Making Parameterized Threads in C#


Posted Date:     Category: C# Syntax    
Author: Member Level: Gold    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();





Did you like this resource? Share it with your friends and show your love!


Responses to "Making Parameterized Threads in C#"
Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Creating and using static constructors
    Previous Resource: Simulation of Dead Lock in Threads
    Return to Resources
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.