0

I can not understand where I'm wrong. First I try to take the length of Stream but throws me a Cancelation Exception. If I try to copy Stream for example stream.CopyTo (memoryStream) it turns out that Stream is Disposed and I can not read it. like hardcode length just to check if running progressbar throws me this error System.ArgumentOutOfRangeException: 'Value of' 200 'is not valid for' Value '. 'Value' should be between 'minimum' and 'maximum'. Parameter name: Value'. I'm still up to nowhere and need to do everything asynchronize. Can anyone help.

private BackgroundWorker _worker;
    public Form1()
    {
        InitializeComponent();
        this._ftpInfo = new FtpInfo();
        this._worker = new BackgroundWorker();

        this._worker.WorkerSupportsCancellation = true;
        this._worker.WorkerReportsProgress = true;

        this._worker.ProgressChanged += backgroundWorker1_ProgressChanged;
        this._worker.DoWork += backgroundWorker1_DoWork;


    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string result = Get_Data_From_Ftp_Server();
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        pb_Ftp.Value += e.ProgressPercentage;
        lbl_Percents.Text = $"{pb_Ftp.Value.ToString()}%";
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        lbl_Percents.Text = "Complete!";
    }



    private string Get_Data_From_Ftp_Server()
    {
        //result data from file

        string result = string.Empty;

        //make request
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(this._ftpInfo.Server + "/" + this._ftpInfo.FileName);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(this._ftpInfo.Username, this._ftpInfo.Password);

        try
        {
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            using (Stream stream = response.GetResponseStream())
            {
                using (FileStream fs = new FileStream(Path.Combine(this._ftpInfo.SaveDirectory, this._ftpInfo.FileName), FileMode.CreateNew, FileAccess.Write))
                {

                    byte[] buffer = new byte[1024];
                    int byteRead = 0;
                    double read = 0;

                    do
                    {

                        byteRead = stream.Read(buffer, 0, 1024);
                        fs.Write(buffer, 0, byteRead);
                        read += (double)byteRead;
                        double percentage = read / 24 * 100;
                        _worker.ReportProgress((int)percentage);


                    }
                    while (byteRead != 0);



                }

            }



            result = response.StatusDescription;
            response.Close();

        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message.ToString());
        }

        return result;

    }
    private void btn_Download_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog dialog = new FolderBrowserDialog();
        dialog.ShowDialog();
        this._ftpInfo.SaveDirectory = dialog.SelectedPath;
        this._ftpInfo.Server = text_Server.Text.Trim();
        this._ftpInfo.FileName = text_Ftp_FileName.Text.Trim();
        this._ftpInfo.Username = text_Username.Text.Trim();
        this._ftpInfo.Password = text_Password.Text.Trim();

        this._worker.RunWorkerAsync();
    }



    private static void Check_Directory(string directory)
    {
        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);
    }


}
  • One problem that I can see is that your background thread is trying to update the UI directly. This will not work. See [How do I update the GUI from another thread?](https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread) for solutions. – RobertBaron Jun 02 '19 at 14:29
  • Is "pb_Ftp.Value += e.ProgressPercentage;" correct? You are updating deltas? Not absolute values? – Sven Bardos Jun 06 '19 at 08:26

0 Answers0