我有一个程序可以下载文件,然后在基于 Windows 的机器上替换旧文件。
每当按下按钮下载文件时,我都会运行两个后台工作人员。一名后台工作人员负责实际执行新文件的 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.";
}