2013-05-01 57 views
0

我正在編寫一個用於上傳和下載文件的C#應用​​程序。下載使用WebClient對象及其DownloadAsycDownload方法。下載適用於多個文件。它下載儘可能多的文件,我想要的。C#多個進度條用於多個文件下載

我的問題是我無法顯示所有文件在不同進度條上的動態添加到窗體的flowlayout control的進度。

這裏是我的代碼:

public ProgressBar[] bar; 
public int countBar=0; 

... 

    bar[countBar] = new ProgressBar(); 
    flowLayoutPanel1.Controls.Add(bar[countBar]); 
    countBar++; 

    request.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownoadInProgress); 
    request.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted); 
    request.DownloadFileAsync(new Uri(this.uri), localPath); 

    byte[] fileData = request.DownloadData(this.uri); 
    FileStream file = File.Create(localPath); 
    file.Write(fileData, 0, fileData.Length); 
    file.Close(); 
} 

public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
{ 
    flowLayoutPanel1.Controls.Remove(bar[countBar]); 
    countBar--; 
    MessageBox.Show("Download Completed"); 
} 

public void DownoadInProgress(object sender, DownloadProgressChangedEventArgs e) 
{ 
    bar[countBar].Maximum = total_bytes; 
    bar[countBar].Value = (int)e.BytesReceived;     
} 

回答

1

您使用的計數索引到進度條,但一旦一個完成 - 刪除最後一個,你真的應該刪除相關聯的一個與文件。

我建議,在這種情況下,使用Dictionary<WebClient, ProgressBar>(可能不是WebCliet - 應該是事件中的sender的類型)。

... 
var progBar = new ProgressBar(); 
progBar.Maximum = 100; 
flowLayoutPanel1.Controls.Add(progBar); 

request.DownloadProgressChanged += DownoadInProgress; 
request.DownloadFileCompleted += DownloadFileCompleted; 
request.DownloadFileAsync(new Uri(this.uri), localPath); 

dic.Add(request, progBar); 

// You shouldn't download the file synchronously as well! 
// You're already downloading it asynchronously. 

// byte[] fileData = request.DownloadData(this.uri); 
// FileStream file = File.Create(localPath); 
// file.Write(fileData, 0, fileData.Length); 
// file.Close(); 

然後,您可以完全刪除countBar,並有新的方法:

public void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 
{ 
    // Can remove (WebClient) cast, if dictionary is <object, ProgressBar> 
    var request = (WebClient)sender; 
    flowLayoutPanel1.Controls.Remove(dic[request]); 
    dic.Remove(request); 
    MessageBox.Show("Download Completed"); 
} 

public void DownoadInProgress(object sender, DownloadProgressChangedEventArgs e) 
{ 
    var progBar = dic[(WebClient)sender]; 
    progBar.Value = e.ProgressPercentage;     
} 
+0

謝謝,這真的很有幫助。 – Habtamu 2013-05-02 12:10:38

+0

如果這完全幫助你,請將其標記爲已接受的答案。否則,讓我知道缺少的東西。 – SimpleVar 2013-05-02 13:55:44

+0

我想從另一個類中添加組件。在運行時無法將進度條添加到表單中。我發佈的這些方法在另一個類中。 – Habtamu 2013-05-02 14:41:03

1

我會用這樣的事情與lambda表達式,有點更簡潔,沒有必要在詞典:

var webClient = new WebClient(); 

var pb = new ProgressBar(); 
pb.Maximum = 100; 
flowLayoutPanel1.Controls.Add(pb); 

webClient.DownloadProgressChanged += (o, args) => 
{ 
    pb.Value = args.ProgressPercentage; 
}; 

webClient.DownloadFileCompleted += (o, args) => 
{ 
    flowLayoutPanel1.Controls.Remove(pb); 
}; 

webClient.DownloadFileAsync(new Uri(this.uri), localPath);