Question

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();
Était-ce utile?

La solution

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();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top