简体   繁体   中英

C# Background Worker Issue

I have a program that downloads a file and then replaces the older one on a Windows based machine.

I have two background workers that get ran whenever a button is pushed to download the file. One background worker is responsible for actually doing the SFTP download of the new file. The other background worker just reads the downloaded file size every second to determine the download progress.

The issue I am having is that on literally half of the machines, the download progress does not display. The download still goes on though. I can not understand why running the same program on two computers, the download progress will show on one computer but will not show on the other.

 // 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.";
    }        

Ivan Stoev had the answer. I needed to put a try catch statement around my FileInfo.Length statement.

He originally wrote:

Are you sure backgroundWorker1_DoWork does not end with exception? For instance if this line long file_size = new FileInfo(@"C:\\BioERemote\\BioR.mdb").Length; throws.

我首先要确保.NET的版本在机器上是最新的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM