首先,我想讓大家都知道,我開始學習C#作爲我的第一種編程方法,所以我是一個絕對的初學者最開心的人,他在2個月前開始編碼,所以請裸露跟我。如何在事件外使用事件參數
所以我試圖做的是創建一個自定義的下載應用程序。它的所有工作除了下載全部按鈕,無法從「DownloadProgressChangedEventArgs」中選取「percent1」變量。我在mainForm構造函數之前實例化了它,但它不會讀取更改的值。
下面的代碼,因爲大多數的它不是有關的問題部分剝離:
public partial class Main : Form
{
//Variables (not all, just the one im having issues with)
private double percentage1;
//Main form constructor
public Main(){...}
//Download File Async custom method
public void DldFile(string url, string fileName, string localPath, AsyncCompletedEventHandler completedName, DownloadProgressChangedEventHandler progressName)
{
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri(url), localPath + "\\" + fileName);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(completedName);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(progressName);
}
//Button 1 click event to start download
private void btnDld1_Click(object sender, EventArgs e)
{
if (url1 != "" && Directory.Exists(localPath1))
{
_startDate1 = DateTime.Now;
DldFile(url1, fileName1, localPath1, completed1, progress1);
}
//took out the try/catch, other ifs to try and cut it down
}
//Download Progress Changed event for Download 1
public void progress1(object sender, DownloadProgressChangedEventArgs e)
{
percentage1 = e.ProgressPercentage; //THIS IS WHERE I WAS EXPECTING TO UPDATE "percentage1"
progressBar1.Value = int.Parse(Math.Truncate(percentage1).ToString());
}
//Button that starts all downloads click event where all my problems are at the moment
private void btnDldAll_Click(object sender, EventArgs e)
{
//The progress bar that should let me know the global status for all webClients
progressBarAll.Value = (
int.Parse(Math.Truncate(percentage1).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage2).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage3).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage4).ToString()) + //HERE IS MY PROBLEM
int.Parse(Math.Truncate(percentage5).ToString()))/5; //HERE IS MY PROBLEM
//Checks if the link exists and starts it from the download button click event
if (url1 != "")
{
btnDld1.PerformClick();
}
//Continues for url2, 3, 4, 5 and else
}
}
所以這是我找到讓你的最短途徑知道我在試圖拉斷,如果有什麼東西失蹤請讓我知道,我會盡可能快地添加任何信息。
我試圖實例化「progress1」嘗試和訪問其percentage1變量,但它沒有奏效。我試着用webClient做同樣的事情,但也沒有工作。我用谷歌和stackflow搜索無濟於事。所以我不知道這個問題是否太愚蠢,或者有一種不同的方式來看待完全不在我心中的問題。
所以主要問題是更新「百分比1」變量並使用它。 關於「progressBarAll.Value」計算還有其他一些問題,當我能夠掌握正確的值時,這些問題將得到解決。所以如果你看到它,就不用擔心。
我很抱歉這麼長的帖子,但我不知道問題出在哪裏,所以我不得不給你更多的信息,以確保我沒有搞錯別的。
快樂編碼; d
好吧,這是一個很好的問題,我不想添加它,所以這篇文章只針對一個問題,然後我會試圖找出更進一步。但是這個想法是根據任何正在運行的下載來保持「progressBarAll」更新。如果dld1正在進行,那麼它會顯示download1進度,如果超過1,它將顯示所有工作下載的平均值。但我想我可以把它關閉一旦我得到一個事件處理程序外的e.Percentage值的hanlde。 –
它究竟是不是工作,DownloadProgressChanged事件從未被解僱,或變量從未分配?嘗試在'progress1()'上放置一個斷點,看它是否被命中。然後逐步調試以追查實際問題。據我所知,您在這裏沒有做錯任何事情,您應該可以從事件處理程序中設置percentage1而不會出現任何問題。 – aevitas
@Aevitas - 我按照你所說的設置了斷點,而我所看到的讓我更加困惑,發生了一些奇怪的事情。當我點擊下載時,所有的事件處理程序被觸發。但是變量'percentage1'與'e.ProgressPercentage'事件參數有一個不同的值,即使我在'progress1()'中聲明'percentage1 = e.ProgressPercentage'。即使我可以看到「百分比*」的值都在變化,progressBarAll根本不會移動。 –