2

I have a method in class A which I want to thread start it in class B by passing parameters.

class ClassA
{
  public void MethodA(string par1, int par2)
  {
    Console.WriteLine("Parameter " + par1 + " is passed with value " + par2);
  }
}

class ClassB
{
  public static void Main(string[] args)
  {
    ClassA obj = new ClassA();
    Thread workerThread = new Thread(new ParameterizedThreadStart(obj.MethodA));
    workerThread.Start("book",5);
  }
}

But when I execute the code it gave me an error `

No overload for 'MethodA' matches delegate 'System.Threading.ParameterizedThreadStart

What is the problem?

Michael
  • 85
  • 6

5 Answers5

3

Try this...

void Main()
{
    ClassA obj = new ClassA();
    Thread thread = new Thread(() => obj.MethodA("Param1", 2));
    thread.Start();
    thread.Join();
}


class ClassA
{
    public void MethodA(string par1, int par2)
    {
        Console.WriteLine("Parameter " + par1 + " is passed with value " + par2);
    }
}

The Issue...

As others have already stated, you have too many parameters...

Other than that you're also not waiting for the thread to complete it's operation.

What you see above is called a lambda expression, basically an anonymous function, and you could compose it in a number of ways, the above is just one example.

Here's another...

void Main()
{
    ClassA obj = new ClassA();
    Thread thread = new Thread(()=> 
    {
        Console.WriteLine ("About to execute MethodA");
        obj.MethodA("test", 2);
        Console.WriteLine ("Executed Method A");
    });
    thread.Start();
    thread.Join();
}


class ClassA
{
    public void MethodA(string par1, int par2)
    {
        Console.WriteLine("Parameter " + par1 + " is passed with value " + par2);
    }
}

If you need further help, let me know

Community
  • 1
  • 1
Aydin
  • 15,016
  • 4
  • 32
  • 42
1

ParameterizedThreadStart only accept a method with one object parameter:

https://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart(v=vs.110).aspx

Kzryzstof
  • 7,688
  • 10
  • 61
  • 108
1

It means exactly what it says. ParameterizedThreadStart has a signature that takes a single parameter of type object. MethodA takes two parameters, a string and an int.

Define a struct for your params and pass that. Rewrite MethodA to expect one param, the struct. That will give you a compatible signature.

And stop creating your own thread. Use the thread pool.

ThreadPool.QueueUserWorkItem((WaitCallback)((new { par1 = par1expr, par2 = par2expr }) => {
  //code that was once the body of MethodA
}));
Peter Wone
  • 17,965
  • 12
  • 82
  • 134
0

ParameterizedThreadStart represents a function with a single parameter of type object.

Consider creating a state object that contains both parameters as properties. You can then pass the state object to MethodA, and MethodA can deconstruct it into individual fields as appropriate.

e.g.

public void MethodA(object paramsThing)
{
    var tuple = (Tuple<string, int>)paramsThing;
    var par1 = tuple.Item1;
    var par2 = tuple.Item2;
    // etc.
}

...

var workerThread = new Thread(new ParameterizedThreadStart(obj.MethodA));
workerThread.Start(Tuple.Create("book",5));
yaakov
  • 5,552
  • 35
  • 48
0

As said ParameterizedThreadStart represents a function with a single parameter of type object. But you don't need it actually. You can use Lambda expressions:

class ClassA
{
    public void MethodA(string par1, int par2)
    {
        Console.WriteLine("Parameter " + par1 + " is passed with value " + par2);
    }
}

class ClassB
{
    public static void Main(string[] args)
    {
        ClassA obj = new ClassA();
        Thread workerThread = new Thread(() => obj.MethodA("par1", 42));
        workerThread.Start();
    }
}
Jesús López
  • 8,338
  • 7
  • 40
  • 66