424,256 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,256 IT Pros & Developers. It's quick & easy.

c# report progress to backgroundworker from another class

P: 1
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 backgroundworker in my main class to call openload function and provide file location.

Expand|Select|Wrap|Line Numbers
  1. private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  2.             {
  3.                 if (!string.IsNullOrEmpty(File))
  4.                 {
  5.                     var Link = new FileHost.Openload();
  6.                     OutputBox.Text = Link.Upload(File);
  7.                 }
  8.             }
Then it goes to openload class where it check authentication and provide upload url to upload class.

Expand|Select|Wrap|Line Numbers
  1. Upload Upload = new FileHost.Upload();
  2.                     Thread Uploading = new Thread(() =>
  3.                     {
  4.                         UploadResult = Upload.UploadFilesToRemoteUrl(UploadURL, FileName, null);                        
  5.                     });
  6.                     Uploading.Start();
in my Upload class it performs uploading in stream format.

Expand|Select|Wrap|Line Numbers
  1. using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
  2.                 {
  3.                     var buffer = new byte[1024];
  4.                     int totalReadBytesCount = 0;
  5.                     var bytesRead = 0;
  6.                     while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  7.                     {
  8.                         memStream.Write(buffer, 0, bytesRead);
  9.                         totalReadBytesCount += bytesRead;
  10.                         var progress = totalReadBytesCount * 100.0 / fileStream.Length;
  11.                     }
  12.                 }
It also provides progress of upload. but i want to provide value of progress to backgroundworker reportprogress.
but i can't call main class backgroundworker to working class i.e. upload class.

is there any method or delegate to call backgroundworker of main class in another class.
Oct 7 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.