简体   繁体   中英

ftp with download progress bar is not filling c#

hi people I'm trying to put a download progress bar to my ftp download program it kinda works but progress bar is not filling with the download or after download but i get the download completed message the code is below ;

public void yap(object o)
    {
        (o as Label).Text = "DOWNLOADING";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
        ParameterizedThreadStart p = new ParameterizedThreadStart(yap);
        Thread t = new Thread(p);
        t.Start(label2);
       BackgroundWorker _backgroundWorker = new BackgroundWorker();
        _backgroundWorker.DoWork += _backgroundWorker_DoWork;
        _backgroundWorker.WorkerReportsProgress = true;
        _backgroundWorker.ProgressChanged +=
   new   ProgressChangedEventHandler(_backgroundWorker_ProgressChanged);
        _backgroundWorker.RunWorkerCompleted += 
 new RunWorkerCompletedEventHandler(_backgroundWorker_RunWorkerCompleted);
        _backgroundWorker.RunWorkerAsync();


    }

    void _backgroundWorker_RunWorkerCompleted
 (object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Download Completed");
    }

    void _backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        AutoSizeMode = AutoSizeMode.GrowAndShrink;
        progressBar1.Maximum = 100;
    } 

    void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {

      Dispatcher.CurrentDispatcher.Invoke
 (System.Windows.Threading.DispatcherPriority.Background,new Action(delegate()
            {

                string yol = Environment.CurrentDirectory;

                FtpWebRequest FTP;
                try
                {

                    FileStream SR = new FileStream(yol + "\\list.gz", FileMode.Create);

                    FTP = (FtpWebRequest)FtpWebRequest.Create(new Uri
("ftp://" + textBox1.Text + "/" + "/usr/valapp/etc/list.gz"));

                    FTP.Credentials = new NetworkCredential("username", "password");

                    FTP.Method = WebRequestMethods.Ftp.DownloadFile;

                    FTP.UseBinary = true;

                    FtpWebResponse response = (FtpWebResponse)FTP.GetResponse();

                    Stream ftpStream = response.GetResponseStream();

                    long cl = response.ContentLength;

                    int bufferSize = 2048;
                    int readCount;

                    byte[] buffer = new byte[bufferSize];
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    while (readCount > 0)
                    {
                        SR.Write(buffer, 0, readCount);
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }
                    ftpStream.Close();
                    SR.Close();
                    response.Close();
                    MessageBox.Show("File Downloaded!");
                    for (int i = 0; i <= 100; i++)
                    {
                        backgroundWorker1.ReportProgress(i);

                        System.Threading.Thread.Sleep(20);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }


            }));
    }

 }
 } 

Code might be look complex but its totally works as i told only progress bar is not filling thank you.

            int cnt=0;
            double totalentry=response.ContentLength/bufferSize;
            while (readCount > 0)
            {

                SR.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);

                if(_backgroundWorker.WorkerReportsProgress) 
                {
                    _backgroundWorker.ReportProgress(cnt++*100/(int)totalentry);
                }

            }

You may just edit this part of the code.

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