Reputation: 180
I am trying to learn multithreading in C#. I have a HW assignment in which we are using the producer/consumer example of threading from MDSN (Found here)
From my understanding, ParameterizedThreadStart
is the "same" as ThreadStart
except for ParameterizedThreadStart
having an Object
parameter.
I have the following method, which I want to create a ParameterizedThreadStart
delegate with:
public void ThreadProdRun(int amount)
{
cell.WriteToCell(amount, ref quantity);
}
And my call to ParameterizedThreadStart
is as follows:
Thread producer = new Thread(new ParameterizedThreadStart (prod.ThreadProdRun));
Which doesn't work because the overloads do not match. I cannot use an object in place of the parameter, or int because int is a struct in C#.
Upvotes: 0
Views: 112
Reputation: 5083
Look at ParameterizedThreadStart
signature by pressing F12
in visual studio.
It is public delegate void ParameterizedThreadStart(object amount);
Your ThreadProdRun method must have object as parameter
public void ThreadProdRun(object amount)
{
cell.WriteToCell((int)amount, ref quantity);
}
EDIT Example with lambda expression and closure
int amount = 1;
Thread t = new Thread(() =>
{
Console.WriteLine(amount);
});
Upvotes: 1