如何发送更多的参数在C#BackgroundWorker的进展更改事件 [英] How to send more arguments in C# backgroundworker progressed changed event
问题描述
我了解我们如何可以传递一个变量(progresspercentage)为progresschanged功能,像这样。
I understand how we can pass one variable(progresspercentage) to "progresschanged" function , like so.
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
...
...
worker.ReportProgress(pc);
...
...
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
不过,我想通过更多的变量这一功能,一些事情,如:
But I want to pass more variables to this function, some thing like:
worker.ReportProgress(pc,username,score);
...
...
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
this.currentUser.Value = e.UserName; //as string
this.score.Value = e.UserScore; //as int
}
对不起我是新的C#中,能有人给我。一个例子
sorry I'm new to c#, could someone give me an example.
推荐答案
后台工作部件的ReportProgress方法重载通过率和对象类型的状态值:
The ReportProgress method of background worker component is overloaded to pass percentage and an object typed state value:
public void ReportProgress(int percentProgress, Object userState)
在您的使用要求,您可以连接UserName和用炭分离得分,所以通过userState参数中的多个值;当它被升高拆分它们ProgressChanged()事件内。您还可以创建基于类的使用值填充它,并使用输入参数userState对象传递小产权。
In your usage requirement you can concatenate the UserName and Score with a char separator, and so pass the multiple values inside the userState parameter; and split them inside the ProgressChanged() event when it is raised. You can also create a small property based class- fill it with values and pass using the userState object typed parameter.
有关如何使用重载的方法ReportProgress样本的例子请看看下面的MSDN链接:
For a sample example of how to use the overloaded ReportProgress method, please look at the below MSDN link:
http://msdn.microsoft.com/en-us/library/a3zbdb1t.aspx
这篇关于如何发送更多的参数在C#BackgroundWorker的进展更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!