0
我想用這個方法:我怎樣才能得到返回值時BackgroundWorker的完成
public static bool SynchronizeFiles(string source, string destination)
在
BackgroundWorker
。我定義了BackgroundWorker
並將其添加到workerList
中。比做這個工作一個接一個。
private void CopyIfIsReady(Save save)
{
if (save.isDriveReady && save.isFileChanged && save.isPeriodCompleted)
{
BackgroundWorker BW = new BackgroundWorker();
BW.DoWork += (obj, e) => backgroundWorker1_DoWork(save);
BW.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
workerList.Add(BW);
BW_timer.Start(500);
}
}
private void BWTimerCallBack(object state)
{
if (workerList.Count >= 0)
{
if(!workerList[0].IsBusy)
{
workerList[0].RunWorkerAsync();
}
}
else
{
BW_timer.Stop();
}
}
private void backgroundWorker1_DoWork(Save save)
{
SynchronizeFiles(save.sourceFolder, save.destFolder);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
workerList.RemoveAt(0);
}
但我怎麼能參加backgroundWorker1_RunWorkerCompleted
的SynchronizeFiles
返回值。
您應該使用'Task'和'await'來代替。 – SLaks