Joe
Joe

Reputation: 551

Add progress bar to function that doesn't loop

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.

Upvotes: 1

Views: 253

Answers (1)

Denis Schaf
Denis Schaf

Reputation: 2729

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

Upvotes: 1

Related Questions