我有一个带有进度条的Dialog。当显示对话框时,Backgroundworker应下载两个文件(使用WebClient)并自动将它们复制到指定位置。 在复制新文件之前,如何等待文件下载。
我曾尝试使用await
做一些事情,但我无法将Backgroundworker更改为异步方法。我如何在工作人员中等待下载完成?
运行worker的代码:
private void fmUpdateingDatabaseDialog_Shown(object sender, EventArgs e)
{
device.Connect();
lbInformation.Text = "uploading database to " + device.FriendlyName;
device.Disconnect();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged +=
new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
}
DoWork处理程序中的代码(实际代码中的路径不为空):
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
//download files temporary
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileTaskAsync(new Uri(""), Path.Combine(tempPath + ""));
WebClient client2 = new WebClient();
client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client2_DownloadProgressChanged);
client2.DownloadFileTaskAsync(new Uri(""), Path.Combine(tempPath + ""));
//upload files to phone
device.Connect();
device.TransferContentToDevice(Path.Combine(tempPath+""), folder.Id, folder, true);
device.TransferContentToDevice(Path.Combine(tempPath+""), folder.Id, folder, true);
device.Disconnect();
}
答案 0 :(得分:2)
您可以使用同步WebClient
方法(例如,DownloadFile
代替DownloadFileTaskAsync
),也可以直接使用async
/ await
< em>而不是BackgroundWorker
的。在这种情况下,您主要进行I / O操作,因此async
比BackgroundWorker
更合适。
async
解决方案看起来像这样:
private async void fmUpdateingDatabaseDialog_Shown(object sender, EventArgs e)
{
device.Connect();
lbInformation.Text = "uploading database to " + device.FriendlyName;
device.Disconnect();
var progress = new Progress<T>(data =>
{
// TODO: move worker_ProgressChanged code into here.
});
await DownloadAsync(progress);
// TODO: move worker_RunWorkerCompleted code here.
}
private async Task DownloadAsync(IProgress<T> progress)
{
//download files temporary
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
await client.DownloadFileTaskAsync(new Uri(""), Path.Combine(tempPath + ""));
WebClient client2 = new WebClient();
client2.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client2_DownloadProgressChanged);
await client2.DownloadFileTaskAsync(new Uri(""), Path.Combine(tempPath + ""));
//upload files to phone
// TODO: Check for Async versions of these methods that you can await.
// If there aren't any, consider using Task.Run.
device.Connect();
device.TransferContentToDevice(Path.Combine(tempPath+""), folder.Id, folder, true);
device.TransferContentToDevice(Path.Combine(tempPath+""), folder.Id, folder, true);
device.Disconnect();
}
答案 1 :(得分:1)
你可以使用这样的东西,它使用"wait and pulse"机制来延迟代码,直到你的下载操作完成:
var locker = new object();
Thread t = new Thread(new ThreadStart(() =>
{
lock (locker)
{
//peform your downloading operation, and wait for it to finish.
client.DownloadFileTaskAsync(new Uri(""), Path.Combine(tempPath + ""));
while (/* not yet downloaded */) { };
//inform the parent thread that the download has finished.
Monitor.Pulse(locker);
}
}));
t.Start();
lock(locker)
{
Monitor.Wait(locker);
}
但是,如果你有资源,我建议重构你的代码以完全使用async-await方法(从而避免后台工作者)。后台工作程序是遗留异步方法之一,而推荐方法是TAP。
请参阅Stephen Cleary的答案,了解如何执行此操作的示例。