现在我有

class MyParamClass
{
   all the parameters I need to pass to the task
}

MyParamClass myParamObj = new MyParamClass();
myParamObj.FirstParam = xyz;
myParamObj.SecondParam = abc;
mytask = new Task<bool>(myMethod, myParamObj,_cancelToken);
mytask.Start()

bool myMethod(object passedMyParamObj)
{
   MyParamClass myParamObj = passedMyParamObj as MyParamClass;
  //phew! finally access to passed parameters
}

无论如何,我无需创建类MyParamClass就可以做到这一点?没有这个杂物,如何将多个参数传递给任务?这是标准做法吗?谢谢你

最佳答案

您可以使用lambda或内联委托(delegate)来执行此操作:

myTask = new Task<bool>(() => MyMethod(xyz, abc), _cancelToken);

09-06 18:56