我想将一个额外的参数传递给我的DoWork
方法,但我得到的编译错误为No overload for 'backgroundWorker1_DoWork' matches delegate 'System.ComponentModel.DoWorkEventHandler'
这是我的语法,我该怎么做才能解决这个问题?
namespace Testing
{
public partial class Form1 : Form1
{
public static string[] employeeName;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1WorkerSupportsCancellation = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
private void btn1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(employeeName);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e, string[] employeeName)
{
//Just for example sake
for (int q = employeeName.GetLowerBound(0); q <= employeeName.GetUpperBound(0); q++)
{
MessageBox.Show(employeeName[q];
}
}
}
}
答案 0 :(得分:3)
DoWorkEventHandler具有以下定义:
public delegate void DoWorkEventHandler(
Object sender,
DoWorkEventArgs e
)
您无法添加第三个参数。您想要获取的对象位于DoWorkEventArgs.Argument
属性中。
public partial class Form1 : Form1
{
public static string[] employeeName;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1WorkerSupportsCancellation = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
private void btn1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(employeeName);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string[] employeeName = (string[])e.Argument;
//Just for example sake
for (int q = employeeName.GetLowerBound(0); q <= employeeName.GetUpperBound(0); q++)
{
MessageBox.Show(employeeName[q];
}
}
}