1

I have a function that exports files to a remote server, I call this static method on a click event from the main form.

private void btnExport_Click(object sender, EventArgs e)
{
   Helper.exportFiles(loggedUser, loggedPass, sourceFolder, destinationRep);
}

I am asked to add a progress bar since it could take a very long time, most examples that I saw here involve the method having some kind of loop. Is it even feasible in my case? since I am calling the method only once.

Joe
  • 461
  • 1
  • 3
  • 15
  • 3
    You can't display progress that you can't measure. If your method doesn't provide any way to measure progress and can't be changed to do so, your only real option is to set the `Style` of a `ProgressBar` to `Marquee` and display what is often referred to as a "continuous" or "Infinite" progress bar. It lets the user know that something is happening without telling them how far along that something is. – user18387401 Aug 17 '22 at 05:28
  • 1
    It seems that you have a source folder, which means this is probably miserable. Use a regular ProgressBar (Style=Blocks) where you can measure the progress based on the number (or preferably, size) of files. – 41686d6564 stands w. Palestine Aug 17 '22 at 05:44

1 Answers1

1

Your function "exportFiles" probably calls some kind of copy function. Replace the copy function with a function like "copyAndReportProgress" that does the same but also increases a counter. Now all you need to know is how many files you will copy. Set this to your progressbar maximum and the reported filecount as your value.

If you do not know how many files you will copy all you can do is use a indeterminate progressbar that does not show the prograss but rather shows that something is happening like this:

<ProgressBar IsIndeterminate="True" />

enter image description here

if you get to the point where people complain about the progressbar not representing the progress properly, you have stubled across one of the oldest problems out there. In this case i recommend a quick watch here: https://www.youtube.com/watch?v=iZnLZFRylbs

Denis Schaf
  • 2,478
  • 1
  • 8
  • 17
  • AFAIK, it copies folders first then files, so it has 2 foreach loops. Assuming I get the authority to put 2 counters there. How can I get their value here? – Joe Aug 17 '22 at 06:07
  • Every time you copy a file you you increase the counter. Just like that. It doesnt matter where you do it in which loop – Denis Schaf Aug 17 '22 at 11:30
  • @Joe been able to solve it? You need to perform the foreach loop twice. Once to count the amount of files you'll copy to use as the progress-bars upper limit and again to actually copy the files. If you did find a solution please share it with the community so others can learn from it – Denis Schaf Aug 22 '22 at 11:38
  • 1
    I was told to use delegates to get the progress, still trying to make it work. – Joe Aug 22 '22 at 11:51