2

我想知道对于我正在尝试做的事情,最好的方法是什么。我的应用程序有一个按钮,用于启动后台下载操作,然后根据下载的文件执行一些操作。

这就像“一旦下载了必要的数据,游戏将很快开始”的情况,用户仍然可以使用主窗体。

private void btnStart_Click(object sender, EventArgs e)
{
    //execute some code

    Downloader.RunWorkerAsync(files); //this worker reports progress to the main form

    while (Downloader.IsBusy)
        Application.DoEvents();

    //execute some more code
}

我知道这样做根本不好。

我无法同步执行下载代码,因为主窗体需要在下载操作期间保持响应。我也不能将最终代码放入下载完成事件中,因为它被程序的许多其他区域使用,并且必须保持“通用”下载系统。

那么,有没有办法做我想做的事?我对其他异步方法没有任何经验。

4

2 回答 2

2

如果你使用BackgrounWorker你必须正确配置它。BW 有RunWorkerCompleted你必须订阅的事件来处理你的异步工作的完成。

于 2013-01-19T07:18:45.533 回答
1

I think you should use asynchronous programming features of the .net 4.5 framework (await and async).

refer to async programming

于 2013-01-19T07:19:29.013 回答