带参数的函数的 C# 进度条
C# Progress bar with function taking arguments
我制作了一个程序,它获取一些文件,将它们重命名为随机数,然后将其保存到不同的文件夹中。我设法让代码做我想做的事,但是它处理的文件有时非常大并且需要一段时间。这意味着用户有时会认为程序崩溃了。
我想为程序添加一个进度条,以明确它仍在运行并且没有崩溃。
我查看了 another question on how to add a progress bar,但在我的例子中,该函数需要输入,这取决于用户点击的按钮。
我不确定如何将它添加到我的代码中。理想情况下,我可以让 backgroundWorker_DoWork
接受额外的输入,所以它会是 (object sender, DoWorkEventArgs e, string[] Files)
,但似乎不可能。
如有任何帮助,我们将不胜感激。
相关函数的代码如下:
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);
}
考虑到您的代码,我建议使用 async/await
,您可以 google 了解其工作原理的详细信息。对于这些,您只能像这样更改 blind
函数:
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);
}
这将保证 blind
函数将 运行 异步。不过别忘了添加 someProgressBar
。
这有一些无法抛出异常的缺点,因此请确保在 blind
函数中处理好它们 - 检查是否无法复制文件等
我制作了一个程序,它获取一些文件,将它们重命名为随机数,然后将其保存到不同的文件夹中。我设法让代码做我想做的事,但是它处理的文件有时非常大并且需要一段时间。这意味着用户有时会认为程序崩溃了。
我想为程序添加一个进度条,以明确它仍在运行并且没有崩溃。
我查看了 another question on how to add a progress bar,但在我的例子中,该函数需要输入,这取决于用户点击的按钮。
我不确定如何将它添加到我的代码中。理想情况下,我可以让 backgroundWorker_DoWork
接受额外的输入,所以它会是 (object sender, DoWorkEventArgs e, string[] Files)
,但似乎不可能。
如有任何帮助,我们将不胜感激。
相关函数的代码如下:
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);
}
考虑到您的代码,我建议使用 async/await
,您可以 google 了解其工作原理的详细信息。对于这些,您只能像这样更改 blind
函数:
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);
}
这将保证 blind
函数将 运行 异步。不过别忘了添加 someProgressBar
。
这有一些无法抛出异常的缺点,因此请确保在 blind
函数中处理好它们 - 检查是否无法复制文件等