SSH.Net Async file download

Ich versuche, Dateien mithilfe von SSH.NET asynchron von einem SFTP-Server herunterzuladen. Wenn ich es synchron mache, funktioniert es einwandfrei, aber wenn ich es asynchron mache, bekomme ich leere Dateien. Das ist mein Code:

var port = 22;
string host = "localhost";
string username = "user";
string password = "password";
string localPath = @"C:\temp";

using (var client = new SftpClient(host, port, username, password))
{
    client.Connect();
    var files = client.ListDirectory("");

    var tasks = new List<Task>();

    foreach (var file in files)
    {                        
        using (var saveFile = File.OpenWrite(localPath + "\\" + file.Name))
        {
            //sftp.DownloadFile(file.FullName,saveFile); <-- This works fine
            tasks.Add(Task.Factory.FromAsync(client.BeginDownloadFile(file.FullName, saveFile), client.EndDownloadFile));
        }                        
    }

    await Task.WhenAll(tasks);
    client.Disconnect();

}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage