c# report progress to backgroundworker from another class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CoolDevil
    New Member
    • Oct 2018
    • 1

    c# report progress to backgroundworker from another class

    i'm working on uploading file to filehost. i'm using listgridview to show filename and its size and also progress bar for each individual file like below.




    I'm performing upload in class manner.

    From main class i'm calling openload class to provide File location. In openload upload class i check authentication and gets upload adress and send that upload adress to another class named Upload which performs multipart/form-data upload.

    Everything is working fine without using progress bar. But i want to implement progress bar to run sucessfully.

    I'm using backgroundworke r in my main class to call openload function and provide file location.

    Code:
    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
                {
                    if (!string.IsNullOrEmpty(File))
                    {
                        var Link = new FileHost.Openload();
                        OutputBox.Text = Link.Upload(File);
                    }
                }
    Then it goes to openload class where it check authentication and provide upload url to upload class.

    Code:
    Upload Upload = new FileHost.Upload();
                        Thread Uploading = new Thread(() =>
                        {
                            UploadResult = Upload.UploadFilesToRemoteUrl(UploadURL, FileName, null);                        
                        });
                        Uploading.Start();
    in my Upload class it performs uploading in stream format.


    Code:
    using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                    {
                        var buffer = new byte[1024];
                        int totalReadBytesCount = 0;
                        var bytesRead = 0;
                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            memStream.Write(buffer, 0, bytesRead);
                            totalReadBytesCount += bytesRead;
                            var progress = totalReadBytesCount * 100.0 / fileStream.Length;
                        }
                    }
    It also provides progress of upload. but i want to provide value of progress to backgroundworke r reportprogress.
    but i can't call main class backgroundworke r to working class i.e. upload class.

    is there any method or delegate to call backgroundworke r of main class in another class.
Working...