먼저 프로그래밍에 대한 첫 번째 접근 방식으로 C#을 배우기 시작 했으므로 2 개월 전부터 코딩을 시작한 절대 초보자 인 hobbiest를 알려 주시기 바랍니다. 나랑.이벤트 외부에서 이벤트 인수를 사용하는 방법
그래서 무엇을 하려는지 사용자 정의 다운로드 응용 프로그램을 만드는 것입니다. 다운로드 버튼을 제외한 모든 버튼은 "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 검색을 아무 소용이 사용했습니다. 그래서 질문이 너무 멍청하거나, 내 사고 방식에서 완전히 그 문제를 보는 다른 방법이 있는지 확실하지 않습니다.
그래서 주된 문제는 "percentage1"변수를 업데이트하고 사용하는 것입니다. "progressBarAll.Value"계산과 관련하여 다른 문제가 있습니다. 올바른 값으로 손을 잡을 수있을 때 해결됩니다. 그렇게 보더라도 걱정할 필요가 없습니다.
긴 게시물에 대한 미안하지만, 문제가 어디에 있는지 잘 모르겠다. 그래서 나는 다른 곳에서 엉망이되지 않도록 더 많은 정보를 제공해야했다.
해피 코딩 D
글쎄 그건 좋은 질문인데, 나는이 글이 하나의 문제에 대해서만 지시되도록 추가하고 싶지 않았다. 그러나 아이디어는'progressBarAll'이 실행중인 다운로드에 따라 업데이트 된 상태를 유지하는 것이 었습니다. dld1이 진행 중이면 download1 진행률을 표시하고, 1을 초과하면 작업중인 모든 다운로드의 평균을 표시합니다. 그러나 나는 이벤트 처리기 외부의 e.Pcentcentage 값에 hanlde를 얻으면이를 해제 할 수 있다고 생각합니다. –
정확히 작동하지 않는 경우, DownloadProgressChanged 이벤트가 실행되지 않았거나 변수가 할당되지 않았습니까? 'progress1()'에 중단 점을 두어 충돌이 발생했는지 확인하십시오. 그런 다음 실제 문제를 추적하기 위해 단계별로 디버깅하십시오. 내가 말할 수있는 범위 내에서 여기서 아무런 잘못을하지 않을 것이므로 문제없이 이벤트 처리기에서 percentage1을 설정할 수 있어야합니다. – aevitas
@Aevitas - 당신이 말한 것처럼 브레이크 포인트를 설정하고, 내가봤을 때 더욱 혼란스러워, 뭔가 이상한 일이 일어나고 있습니다. 다운로드를 클릭하면 모든 이벤트 핸들러가 시작됩니다. 그러나'percentage1 = e.ProgressPercentage'라고'progress1()'에 명시되어 있어도 변수'percentage1'는 'e.ProgressPercentage' 이벤트 인수와 다른 값을 갖습니다. 그리고 progressBarAll은 전혀 움직이지 않습니다. 비록 'percentage *'의 값이 모두 변하는 것을 볼 수 있습니다. –