1

Could someone tell me or suggest how I can make the progressbar work with my current codes? I'm decrypting files from directory. I want to show the progress. I tried reporting the inputFiles count but I can't make it work.

Here's my code:

  private async Task Start()
    {
        int bufferSize = Convert.ToInt32(cbBufferSize.Text);
        int count = 0;

        foreach (ListViewItem lvi in LV.Items)
        {
            string inputhPaths = lvi.SubItems[0].Text;
            string keyPaths = lvi.SubItems[1].Text;
            outputPaths = Path.Combine(lvi.SubItems[2].Text, lvi.SubItems[3].Text + ".ts");

            using (var fsOutput = new FileStream(outputPaths, FileMode.Create, FileAccess.Write))
            {
                foreach (string inputFiles in Directory.GetFiles(inputhPaths, "*.ts"))
                {
                    using (var fsInput = new FileStream(inputFiles, FileMode.Open, FileAccess.Read))
                    {
                        count++;
                        progressBar1.Value = (int)count / inputFiles.Count() * 100;

                        await Task.Run(() => Decrypter.BufferMethod(fsInput, keyPaths, fsOutput, bufferSize));
                    }
                }
            }
        }
    }
zackmark15
  • 657
  • 6
  • 12

2 Answers2

0

The way I approach this is to have my DoWork task method report progress and in my ProgressChanged method that handles progress updates, that's where I update my progressBar value.

Lothar
  • 23
  • 5
  • Yeah I can do it in backgroundworker. But I'm moving now to async/await that's why I'm still learning how to use it properly – zackmark15 Jun 24 '20 at 16:39
0

I just did like this and it worked

int counter = 0; string inputFiles = ...............

counter += 1; 
progressBar1.Value = (int)(counter / (double)inputFiles.Count() * 100); 
zackmark15
  • 657
  • 6
  • 12