Reputation: 61
I have made a program which takes some files, renames them to a random number and then saves it to a different folder. I managed to make the code do what I wanted, however the files it handles are sometimes quite large and take a while. This means the users sometimes think that the program has crashed.
I would like to add a progress bar to the program, to make it obvious that it's still working and not crashed.
I had a look at another question on how to add a progress bar, however in my case the function takes an intake, which depends on the button the user clicks.
I'm unsure how I could add it into my code. Ideally I could have the backgroundWorker_DoWork
take an additional input, so it would be (object sender, DoWorkEventArgs e, string[] Files)
, but it doesn't seem to be possible.
Any help would be greatly appreciated.
The code for the relevant functions is below:
private void blindSelected_Click(object sender, EventArgs e)
{
List<string> toBlind = new List<string>();
// Find all checked items
foreach (object itemChecked in filesToBlind.CheckedItems)
{
toBlind.Add(itemChecked.ToString());
}
string[] arrayToBlind = toBlind.ToArray();
blind(arrayToBlind);
}
private void blindAll_Click(object sender, EventArgs e)
{
List<string> toBlind = new List<string>();
// Find all items
foreach (object item in filesToBlind.Items)
{
toBlind.Add(item.ToString());
}
string[] arrayToBlind = toBlind.ToArray();
blind(arrayToBlind);
}
private void blind(string[] files)
{
// Generate an integer key and permute it
int[] key = Enumerable.Range(1, files.Length).ToArray();
Random rnd = new Random();
int[] permutedKey = key.OrderBy(x => rnd.Next()).ToArray();
// Loop through all the files
for (int i = 0; i < files.Length; i++)
{
// Copy original file into blinding folder and rename
File.Copy(@"C:\test\" + files[i], @"C:\test\blind\" + permutedKey[i] + Path.GetExtension(@"C:\test\" + files[i]));
}
// Show completed result
MessageBox.Show("Operation complete!", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Upvotes: 0
Views: 135
Reputation: 138
Considering your code, I would suggest using async/await
, you can google for details on how it works. With those you can only change your blind
function like this:
private async Task blind(string[] files)
{
// Generate an integer key and permute it
int[] key = Enumerable.Range(1, files.Length).ToArray();
Random rnd = new Random();
int[] permutedKey = key.OrderBy(x => rnd.Next()).ToArray();
// Loop through all the files
for (int i = 0; i < files.Length; i++)
{
// Copy original file into blinding folder and rename
// Notice, this will wait for the Task to actually complete
await Task.Run(() => File.Copy(@"C:\test\" + files[i], @"C:\test\blind\" + permutedKey[i] + Path.GetExtension(@"C:\test\" + files[i])));
someProgressBar.PerformStep(); // Or just set value by yourself
}
// Show completed result
MessageBox.Show("Operation complete!", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
This will guarantee you that blind
function will run asynchronously. Don't forget to add someProgressBar
though.
This has some downsides about not being able to throw exceptions up, so make sure you handle them well inside blind
function - check for not being able to copy file etc.
Upvotes: 1