我有一个程序可以下载文件,然后在基于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.";
}
答案 0 :(得分:1)
Ivan Stoev得到了答案。我需要在FileInfo.Length语句周围添加一个try catch语句。
他最初写道:
你确定backgroundWorker1_DoWork没有以异常结束吗?对于 实例,如果这行long file_size = new 的FileInfo(@" C:\ BioERemote \ BioR.mdb&#34)。长度;抛出。
答案 1 :(得分:-1)
我首先确保.NET的版本在计算机上是最新的。