使用多个参数启动任务的标准做法是什么
现在我有
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 吗?如何在没有这种杂技的情况下将多个参数传递给任务?这是标准做法吗?谢谢
Right now I have
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
}
Is there anyway I can do this without having the need to create class MyParamClass ? How can I pass multiple params to a task without this jugglery ? Is this the standard practice ? thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 lambda 或内联委托来执行此操作:
You can do this with a lambda or inline delegate:
使用包装类来处理是执行此操作的标准方法。否则,您唯一可以做的就是使用
Tuple
来避免编写MyParamClass
。Using a wrapper class to handle is the standard way to do this. The only thing you can do otherwise is use a
Tuple
to avoid writingMyParamClass
.