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.";
}
'backgroundWorker1_DoWork'가 예외로 끝나지 않았습니까? 예를 들어 다음과 같은 줄이 있다면'long file_size = new FileInfo (@ "C : \ BioERemote \ BioR.mdb"). Length;'throws. –
모든 컴퓨터에서 * local_remote_destination *은 @ "C : \ BioERemote \ BioR.mdb"입니까? – Graffito
감사합니다. 이반! 문제는 내가 가지고있는 본사 건물과 모든 사람들이 똑같은 연결성을 가지고 있다고 가정하고 있었다. 그래서 나는 그 한 줄의 코드 주위에서 try catch 문을 써서 문제를 해결했다. 의문점은 프로그램이 결코 오류를 내지 않거나 오류를 던지지 않을 것입니다. 느린 연결에서 backgroundWorker1이 실패하고 단지 오류가 발생하지 않고 그냥 종료되고 backgroundWorker2가 계속 다운로드한다고 가정합니다. – dcfjoe