2017-02-01 8 views
1

Windows 기반 컴퓨터에서 파일을 다운로드 한 다음 이전 프로그램을 대체하는 프로그램이 있습니다.C# 배경 작업자 문제

두 명의 배경 작업자가 버튼을 눌러 파일을 다운로드 할 때마다 실행됩니다. 한 명의 작업자가 실제로 새 파일의 SFTP 다운로드를 담당합니다. 다른 백그라운드 작업자는 다운로드 진행률을 확인하기 위해 매초 다운로드 한 파일 크기를 읽습니다.

내가 겪고있는 문제는 문자 그대로 컴퓨터의 절반에서 다운로드 진행률이 표시되지 않는다는 것입니다. 그래도 다운로드는 계속됩니다. 두 컴퓨터에서 동일한 프로그램을 실행하는 이유를 이해할 수 없지만 다운로드 진행률은 한 컴퓨터에서만 표시되지만 다른 컴퓨터에서는 표시되지 않습니다.

// This thread will handle monitoring the downloaded BioR.mdb file size 
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    {   
     BackgroundWorker worker = sender as BackgroundWorker; // necessary 

     do // continue reporting file size until the file has finished downloading 
     { 
      Thread.Sleep(1000); // report once per second 

      long file_size = new FileInfo(@"C:\BioERemote\BioR.mdb").Length; // get file size 
      worker.ReportProgress(Convert.ToInt32(file_size)); // "worker" reports the file size to ProgressChanged event 
     } while (!is_complete); // is_complete becomes true when backgroundworker2 completes the download   
    } 

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     label2.Text = Convert.ToString(Math.Round((Convert.ToDouble(e.ProgressPercentage)/Convert.ToDouble(remote_size)) * 100.0, 2)) + "% Downloaded"; 
    } 

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     label2.Text = ""; 
    } 

    private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) 
    { 
     using (var client = new SftpClient(sftp_address, sftp_username, sftp_password)) 
     { 
      client.Connect(); 
      DownloadDirectory(client, bioe_remote_source, local_remote_destination); 
      client.Disconnect(); 
     } 

     is_complete = true; 
    } 

    private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    {    
     label_Status.ForeColor = Color.Green; 
     label_Status.Text = "Completed! You may use Remote."; 
    }   
+2

'backgroundWorker1_DoWork'가 예외로 끝나지 않았습니까? 예를 들어 다음과 같은 줄이 있다면'long file_size = new FileInfo (@ "C : \ BioERemote \ BioR.mdb"). Length;'throws. –

+0

모든 컴퓨터에서 * local_remote_destination *은 @ "C : \ BioERemote \ BioR.mdb"입니까? – Graffito

+0

감사합니다. 이반! 문제는 내가 가지고있는 본사 건물과 모든 사람들이 똑같은 연결성을 가지고 있다고 가정하고 있었다. 그래서 나는 그 한 줄의 코드 주위에서 try catch 문을 써서 문제를 해결했다. 의문점은 프로그램이 결코 오류를 내지 않거나 오류를 던지지 않을 것입니다. 느린 연결에서 backgroundWorker1이 실패하고 단지 오류가 발생하지 않고 그냥 종료되고 backgroundWorker2가 계속 다운로드한다고 가정합니다. – dcfjoe

답변

-1

컴퓨터의 .NET 버전이 최신 버전인지 확인해야합니다.

+0

Visual Studio에서 .NET Framework 4로이 빌드를 대상으로했습니다. 작동하지 않는 컴퓨터에 최신 .NET Framework 4.6.2를 설치했습니다. 이것은 문제를 해결하지 못했습니다. – dcfjoe

1

이반 스 토프 (Ivan Stoev)가 대답했습니다. 내 FileInfo.Length 문 주위에 try catch 문을 넣어야했습니다.

그는 원래 썼다 :

당신은 backgroundWorker1_DoWork 예외로 끝나지 않도록 있습니까? 이 행이 긴 경우 인스턴스의 경우 file_size = new FileInfo (@ "C : \ BioERemote \ BioR.mdb"). 길이; 던졌습니다.