如何发送更多的参数在C#backgroundworker进行改变事件 [英] How to send more arguments in C# backgroundworker progressed changed event

查看:159
本文介绍了如何发送更多的参数在C#backgroundworker进行改变事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们如何将一个变量(progresspercentage)传递给进阶交换功能,如下所示。

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;
}

但是我想传递更多的变量到这个函数, / p>

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和Score与char分隔符连接起来,因此传递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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆