namespace BackgroundWorkerExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1000);
MessageBox.Show("Now!");
}
private void button1_Click(object sender, EventArgs e)
{
//Not working friends!
backgroundWorker1.RunWorkerAsync(backgroundWorker1_DoWork);
}
}
}
如何调用DoWork方法(我是否必须这样做?lol )
答案 0 :(得分:3)
backgroundWorker1.RunWorkerAsync();
参数是可选的,用于将参数传递给DoWork:
backgroundWorker1.RunWorkerAsync(10);
backgroundWorker1.RunWorkerAsync(obj); // Pass multiple arguments using an object
可以使用e.Argument强制转换为对象类型从DoWork访问。
答案 1 :(得分:1)
没关系,我自己找到了答案。事实证明该方法没有任何参数用于我的用例。
namespace BackgroundWorkerExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1000);
MessageBox.Show("Now!");
}
private void button1_Click(object sender, EventArgs e)
{
//Now it works!
backgroundWorker1.RunWorkerAsync();
}
}
}