Reputation: 3563
I have created a simple Task
instance in my code and i want to pass index
as the State
parameter for that Task
. I have wrote the following code to archive this. But not working. Can anyone please help me on this?. Thanks in Advance.
int index = 0;
Task<int> task = new Task<int>(() =>
{
return 1;
}, index);
task.Start();
Upvotes: 0
Views: 71
Reputation: 4911
If you want to pass state
parameter to your Task , then your delegate (first argument) should accept this state parameter as its input:
int index = 0;
Task<int> task = new Task<int>((state) => { return 1; }, index);
//Task<int> task = new Task<int>(state => 1, index); // a bit shorter alternative
task.Start();
Upvotes: 3