接受多个参数的BackgroundWorker 的替代方案?
BackgroundWorker 对象允许我们将单个参数传递到 DoWorkEventHandler 中。
// setup/init:
BackgroundWorker endCallWorker = new BackgroundWorker();
endCallWorker.DoWork += new DoWorkEventHandler(EndCallWorker_DoWork);
...
endCallWorker.RunWorkerAsync(userName);
// the handler:
private void EndCallWorker_DoWork(object sender, DoWorkEventArgs e)
{
string userName = e.Argument as string;
...
}
要传递多个参数,我必须将它们包装在一个对象中,就像这个可怜的字符串数组:
// setup/init:
BackgroundWorker startCallWorker = new BackgroundWorker();
startCallWorker.DoWork += new DoWorkEventHandler(StartCallWorker_DoWork);
...
startCallWorker.RunWorkerAsync(new string[]{userName, targetNumber});
// the handler:
private void StartCallWorker_DoWork(object sender, DoWorkEventArgs e)
{
string[] args = e.Argument as string[];
string userName = args[0];
string targetNumber = args[1];
}
是否有另一个对象或模式允许我们很好地传递多个参数,或者理想情况下,编写我们自己的签名?
The BackgroundWorker object allows us to pass a single argument into the DoWorkEventHandler.
// setup/init:
BackgroundWorker endCallWorker = new BackgroundWorker();
endCallWorker.DoWork += new DoWorkEventHandler(EndCallWorker_DoWork);
...
endCallWorker.RunWorkerAsync(userName);
// the handler:
private void EndCallWorker_DoWork(object sender, DoWorkEventArgs e)
{
string userName = e.Argument as string;
...
}
To pass multiple arguments, I must wrap them in an object, like this poor string array:
// setup/init:
BackgroundWorker startCallWorker = new BackgroundWorker();
startCallWorker.DoWork += new DoWorkEventHandler(StartCallWorker_DoWork);
...
startCallWorker.RunWorkerAsync(new string[]{userName, targetNumber});
// the handler:
private void StartCallWorker_DoWork(object sender, DoWorkEventArgs e)
{
string[] args = e.Argument as string[];
string userName = args[0];
string targetNumber = args[1];
}
Is there another object or pattern that allows us pass multiple arguments nicely, or ideally, write our own signature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为什么不让“一个”对象作为参数数组传递? 您只需要将其从对象参数转换回方法内的数组即可。
Why not have the "one" object passed be an array of parameters? You only need to cast it back to array inside the method from the object parameter.
创建一个包含所有参数的类
Create a class that holds all your arguments
也许传递一个 lambda 函数作为你的对象? 然后您可以在 DoWork 处理程序中调用它。
Maybe pass a lambda function as your object? Then you'd call it in the DoWork handler.
对象可以是列表或数组等。 只需使您的对象成为某种容器,然后在BackgroundWorker 中进行转换即可。 不过,您需要确保始终传递相同的类型。
Object can be a list or array or some such. Just make your object a container of some sort, then cast within the BackgroundWorker. You need to make sure you're always passing in the same type though.
而不是类型化的对象。 C# 4.0为我们提供了元组。 我们可以使用一个元组来保存多个参数。 那么就不需要声明一个新的类了。
instead of a typed object. C# 4.0 provides us with tuple. We could use a tuple to hold multiple args. Then there is no need to declare a new class.
使用类型化对象有什么问题?
What's wrong with using a typed object?
您可以使用闭包(Lambda):
或者使用委托(匿名方法)语法:
You could use a closure (Lambda):
Or with delegate (anonymous method) syntax: