views:

672

answers:

2

The execution of the following code yields error :No overloads of ProcessPerson Matches ThreadStart.

public class Test
    {
        static void Main()
        {
            Person p = new Person();
            p.Id = "cs0001";
            p.Name = "William";
            Thread th = new Thread(new ThreadStart(ProcessPerson));
            th.Start(p);
        }

        static void ProcessPerson(Person p)
        {
              Console.WriteLine("Id :{0},Name :{1}", p.Id, p.Name);
        }

    }

    public class Person
    {

        public string Id
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }


    }

How to fix it?

+1  A: 

Your function declaration should look like this:

static void ProcessPerson(object p)

Inside ProcessPerson, you can cast 'p' to a Person object.

Richard Nienaber
+10  A: 

Firstly, you want ParameterizedThreadStart - ThreadStart itself doesn't have any parameters.

Secondly, the parameter to ParameterizedThreadStart is just object, so you'll need to change your ProcessPerson code to cast from object to Person.

static void Main()
{
    Person p = new Person();
    p.Id = "cs0001";
    p.Name = "William";
    Thread th = new Thread(new ParameterizedThreadStart(ProcessPerson));
    th.Start(p);
}

static void ProcessPerson(object o)
{
    Person p = (Person) o;
    Console.WriteLine("Id :{0},Name :{1}", p.Id, p.Name);
}

However, if you're using C# 2 or C# 3 a cleaner solution is to use an anonymous method or lambda expression:

static void ProcessPerson(Person p)
{
    Console.WriteLine("Id :{0},Name :{1}", p.Id, p.Name);
}

// C# 2
static void Main()
{
    Person p = new Person();
    p.Id = "cs0001";
    p.Name = "William";
    Thread th = new Thread(delegate() { ProcessPerson(p); });
    th.Start();
}

// C# 3
static void Main()
{
    Person p = new Person();
    p.Id = "cs0001";
    p.Name = "William";
    Thread th = new Thread(() => ProcessPerson(p));
    th.Start();
}
Jon Skeet
In C# 1 parameter passing is not allowed ?
By declaring Thread th = new Thread(delegate() { ProcessPerson(p); }),the delegate() is automatically converted to type "ParameterizedThreadStart" right?
Cool. Didn't know you could create threads that easily using lambdas :D
Svish
@threadpool: No, that's actually using the parameterless ThreadStart delegate, but it's capturing "p" and using that when the delegate is executed. You really want to read up on lambda expressions and anonymous methods before using them though - there are various subtleties :)
Jon Skeet
oh! I read lambda (3.0) and anonymous methods (2.0),now things are crystal clear.By the way have you written article about multi threading (i.e) http://www.yoda.arachsys.com/csharp/threads/parameters.shtml ?.But your C# in Depth does not cover a lot about Multithreading.
@threadpool: No, it doesn't cover multithreading in detail as that's part of .NET rather than C#. I mostly stick to language features.
Jon Skeet
Well,We are waiting for your book on Threading....
@threadpool: Nope - read Joe Duffy's book instead :)
Jon Skeet