异步方法挂起UI

时间:2016-09-07 08:51:00

标签: c# winforms asynchronous

我有一个窗口,它使用一些异步方法。但是,只有在运行时创建UI时才会挂起UI(在这些操作期间),如下所示:

private void button_Click(object sender, RibbonControlEventArgs e)
{
    AboutWindow SettingsWindow = new AboutWindow();
    SettingsWindow.ShowDialog();
}

但是,如果我这样创建它:

private AboutWindow SettingsWindow = new AboutWindow();
private void button_Click(object sender, RibbonControlEventArgs e)
{
    SettingsWindow.ShowDialog();
}

一切似乎都没问题,用户界面也没有停止。

窗口内导致问题的方法如下所示:

private async void CallUpdateDownload()
{
    UpdaterStatus.CurrentUpdateStatus = await DownloadAndSaveInstallerAsync(UpdaterStatus.freshVersion);
}

public static async Task<UpdateStatus> DownloadAndSaveInstallerAsync(string NewVersion)

主要挂起发生在DownloadAndSaveInstallerAsync内的此字符串中:

CloudBlockBlob blob = new CloudBlockBlob(new Uri(sas));
MemoryStream msRead = new MemoryStream();
blob.DownloadToStream(msRead); // HANGS HERE

问题是,为什么会这样?为什么在启动时创建窗口会阻止它从异步方法挂起。

另一个问题是,我有另一个对象,它也使用DownloadAndSaveInstallerAsync方法。这些对象是在运行时创建的,我无法做任何事情来防止这种挂起,在下载时发生。

如何防止UI在这种情况下挂起(在运行时创建对象)?

以下更多代码:

        private async void CallUpdateDownload()
                {
                    UpdaterStatus.CurrentUpdateStatus = await UpdaterLogic.DownloadAndSaveInstallerAsync(UpdaterStatus.freshVersion);
                }

UpdaterLogic:

        public static async Task<UpdateStatus> DownloadAndSaveInstallerAsync(string NewVersion)
                {
        //...
                          if (await SaveInstallerToMemoryAsync(NewVersion))
                                {
                                        return UpdateStatus.ReadyToInstall;
                                }
                                else
                                {
                                    return UpdateStatus.Error;
                                }
                }

    private static async Task<bool> SaveInstallerToMemoryAsync(string NewVersion)
            {
                        using (MemoryStream ms = await UpdaterClient.DownloadInstallerAsync(NewVersion))
                        using (FileStream fs = new FileStream(UpdaterSettings.Path), FileMode.Create))
                        {
                            if (ms == null) return false;
                            ms.Position = 0;
                            await ms.CopyToAsync(fs);
                            fs.Close();
                            ms.Close();
                            return true;
                        }
            }

UpdaterClient:

    public static async Task<MemoryStream> DownloadInstallerAsync(string version)
            {
                        string sas = await GetInstallerDownloadLinkAsync(version);

                        CloudBlockBlob blob = new CloudBlockBlob(new Uri(sas));

                            MemoryStream msRead = new MemoryStream();
                            blob.DownloadToStream(msRead);
                            return msRead;
            }

public static async Task<string> GetInstallerDownloadLinkAsync(string version)
        { 
                    HttpResponseMessage response = null;
                        response = await httpClient.GetAsync(UpdaterSettings.Link + version);
                    if (response.IsSuccessStatusCode)
                    {
                        var res = await response.Content.ReadAsStringAsync();
                        return res.Trim('"');
                    }
        }

1 个答案:

答案 0 :(得分:1)

您必须在任务/线程中执行长时间运行的部分,并await它们以避免阻塞。

public static async Task<MemoryStream> DownloadInstallerAsync(string version)
{
    string sas = await GetInstallerDownloadLinkAsync(version);

    CloudBlockBlob blob = new CloudBlockBlob(new Uri(sas));

    MemoryStream msRead = new MemoryStream();
    await Task.Run( () => blob.DownloadToStream(msRead) );
    return msRead;
}

<强>更新

CloudBlockBlob确实提供了DownloadToStreamAsync方法,您可以使用

public static async Task<MemoryStream> DownloadInstallerAsync(string version)
{
    string sas = await GetInstallerDownloadLinkAsync(version);

    CloudBlockBlob blob = new CloudBlockBlob(new Uri(sas));

    MemoryStream msRead = new MemoryStream();
    await blob.DownloadToStreamAsync(msRead);
    return msRead;
}