运行 两个 BackgroundWorker 在一起
Running two BackgroundWorkers together
我想 运行 2 个 BackgroundWorker 并行。我该如何实施?在我下面的代码中,backgroundWorker2 不起作用:
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
backgroundWorker2.RunWorkerAsync();
}
改用Task Parallel Library。像这样做事的新方法。
伪代码使用Parallel.Invoke:
Parallel.Invoke(() => SomeMethod(), () => SomeOtherMethod());
伪代码 Task:
async Task SomeMethod() { }
async Task SomeOtherMethod() { }
Task task1 = SomeMethod();
Task task2 = SomeOtherMethod();
await Task.WhenAll(task1,task2);
// get results task1.Result and task2.Result
我想 运行 2 个 BackgroundWorker 并行。我该如何实施?在我下面的代码中,backgroundWorker2 不起作用:
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
backgroundWorker2.RunWorkerAsync();
}
改用Task Parallel Library。像这样做事的新方法。
伪代码使用Parallel.Invoke:
Parallel.Invoke(() => SomeMethod(), () => SomeOtherMethod());
伪代码 Task:
async Task SomeMethod() { }
async Task SomeOtherMethod() { }
Task task1 = SomeMethod();
Task task2 = SomeOtherMethod();
await Task.WhenAll(task1,task2);
// get results task1.Result and task2.Result