Concurrent and non-concurrent downloads: async and await in c#

时间:2017-06-15 10:14:52

标签: c# wpf asynchronous mvvm downloadfileasync

I'm writing a C#, WPF programm with the MVVM-pattern.

I'm trying to first download a few pictures simultaneously (since they are small) and when they all finished downloading, THEN I want to download a few files one after the other. Of course the whole time the GUI can't freeze.

I'm using WebClient and DownloadFileAsync. I guess I have to put the loops in methods and declare them async and use await, but all I tried failed (all downloads always start at the same time).

Thanks a lot

Edit: Since I tried around so much, giving a good sourcecode sample is tough, but here it goes:

public Communication(){
    DownloadPics(downloadInfo);
    DownloadFiles(downloadInfo);
}

private void DownloadPics(List<Dictionary<string, string>> downloadInfo) {
        foreach (Dictionary<string, string> di in downloadInfo) {
            DownloadPic(di);
        }
    }


    private void DownloadBooks(List<Dictionary<string, string>> downloadInfo) {
        foreach (Dictionary<string, string> di in downloadInfo) {                
            DownloadBook(di);
        }
    }

    private void DownloadPic(Dictionary<string, string> di) {
        new PicDownload(new Uri(downloadInfo["urlPic"]), downloadInfo["fileDestPartOne"] + "png");
    }

    private void DownloadBookAsync(Dictionary<string, string> downloadInfo) {
        new BookDownload(new Uri(downloadInfo["urlVersion"]), downloadInfo["fileDestPartOne"] + "ouf");
    }

public PicDownload(...){
    webClientPic.DownloadFileAsync(uri, destination);
}
public FileDownload(...){
    webClientPic.DownloadFileAsync(uri, destination);
}

0 个答案:

没有答案