How do i fix backgroundworker is busy c# code

Bluevels

i keep getting background worker is busy when i run my code. any help?

       struct FtpSetting
    {
        public string Server { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string FileName { get; set; }
        public string FullName { get; set; }
    }

    FtpSetting _inputParameter;

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        string fileName = ((FtpSetting)e.Argument).FileName;
        string fullName = ((FtpSetting)e.Argument).FullName;
        string userName = ((FtpSetting)e.Argument).Username;
        string password = ((FtpSetting)e.Argument).Password;
        string server = ((FtpSetting)e.Argument).Server;
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", server, fileName)));
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(userName, password);
        Stream ftpStream = request.GetRequestStream();
        FileStream fs = File.OpenRead(fullName);
        byte[] buffer = new byte[1024];
        double total = (double)fs.Length;
        int byteRead = 0;
        double read = 0;
        do
        {
            if (!backgroundWorker.CancellationPending)
            {
                //Upload file & update process bar
                byteRead = fs.Read(buffer, 0, 1024);
                ftpStream.Write(buffer, 0, byteRead);
                read += (double)byteRead;
                double percentage = read / total * 100;
                backgroundWorker.ReportProgress((int)percentage);
            }
        }
        while (byteRead != 0);
        fs.Close();
        ftpStream.Close();
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lblStatus.Text = $"Uploaded {e.ProgressPercentage} %";
        progressBar.Value = e.ProgressPercentage;
        progressBar.Update();
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        lblStatus.Text = "Upload complete !";
    }

    private void btnUpload_Click(object sender, EventArgs e)
    {
        string dir = "C:\\Users\\bluevels\\Pictures\\";
        string[] files = Directory.GetFiles(dir);
        foreach (string file in files)
        {

            string FileName = Path.GetFileName(file);

            FileInfo fi = new FileInfo(FileName);
            _inputParameter.Username = txtUserName.Text;
            _inputParameter.Password = txtPassword.Text;
            _inputParameter.Server = txtServer.Text;
            _inputParameter.FileName = fi.Name;
            _inputParameter.FullName = fi.FullName;
            backgroundWorker.RunWorkerAsync(_inputParameter);

        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

i keep getting the error from this line

backgroundWorker.RunWorkerAsync(_inputParameter);
Bluevels

so i figured it our. all i had to do was put the loop in the backgroundworker as seen in the code below.

    struct FtpSetting
    {
        public string Server { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

    }

    FtpSetting _inputParameter;

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        string fileName = "";
        string fullName = "";

        string dir = "C:\\Users\\bluevels\\Desktop\\ww\\";
        string[] files = Directory.GetFiles(dir);
        foreach (string file in files)
        {

            string FileName = "C:\\Users\\bluevels\\Desktop\\ww\\" + Path.GetFileName(file);


            FileInfo fi = new FileInfo(FileName);

            fileName = fi.Name;
            fullName = fi.FullName;





            string userName = ((FtpSetting)e.Argument).Username;
            string password = ((FtpSetting)e.Argument).Password;
            string server = ((FtpSetting)e.Argument).Server;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", server, fileName)));
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(userName, password);
            Stream ftpStream = request.GetRequestStream();
            FileStream fs = File.OpenRead(fullName);
            byte[] buffer = new byte[1024];
            double total = (double)fs.Length;
            int byteRead = 0;
            double read = 0;
            do
            {
                if (!backgroundWorker.CancellationPending)
                {
                    //Upload file & update process bar
                    byteRead = fs.Read(buffer, 0, 1024);
                    ftpStream.Write(buffer, 0, byteRead);
                    read += (double)byteRead;
                    double percentage = read / total * 100;
                    backgroundWorker.ReportProgress((int)percentage);
                }
            }
            while (byteRead != 0);
            fs.Close();
            ftpStream.Close();
        }
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lblStatus.Text = $"Uploaded {e.ProgressPercentage} %";
        progressBar.Value = e.ProgressPercentage;
        progressBar.Update();
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        lblStatus.Text = "Upload complete !";
    }

    private void btnUpload_Click(object sender, EventArgs e)
    {
                _inputParameter.Username = txtUserName.Text;
                _inputParameter.Password = txtPassword.Text;
                _inputParameter.Server = txtServer.Text;
                backgroundWorker.RunWorkerAsync(_inputParameter);


    }

the percentage calculation also works fine with every file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is the constructor in this C++ code ambiguous and how do I fix it?

Why is this code not working, How do i fix it?

How do i use backgroundworker with timer and progressBar?

How do I fix these errors in c++?

How do I fix stop "Cannot implicitly convert type int to bool" error code in c#

How do i change label in another form with backgroundworker process c#?

How do I fix my duplicate detection code? Java

How do i fix my code's vulnerability?

How do I fix my code about a script that stopped working?

Error in Python Code. How do I fix a skip?

how do I fix this code - variable not defined in function

How do I fix my code for plotting a histogram?

How do I fix the indentation error in my code?

How do I fix this code to get the proper output

How do i fix the infinite loop that this code gets itself into?

How do I fix this R code for area under curve?

how do I fix my code in order to display one table?

How do I fix my code with Pygame to jump correctly?

Accidentally deleted flutter default widget code, how do I fix it?

How do I fix the warning in python: This code is unreachable

How do I fix this comment SQL/PHP code?

How do I fix Webpack from mangling my code in production?

How do I fix this html code to make the required table?

Code executing both if and else statements together how do i fix?

How do i fix my class code 'int' object error?

How do I fix my code so that it is automated?

how do i fix this subprocess error in my code

How do I fix comment showing on code in javascript?

How do I fix json file reading code NodeJS Electron