【发布时间】:2014-01-20 23:01:42
【问题描述】:
所以在下面的 sn-p 中,我非常简单地查看特定文件夹并将图像从源复制到目标。
复制速度非常快,它适用于第一批文件夹(可能是 20 个左右),这需要几秒钟。但随后进度条停止移动,我得到一个旋转的鼠标光标。我可以查看目标文件夹,它仍在处理文件夹。
完成后,我得到“进程完成”对话框,进度条为 100%,一切正常。
只是想确保最终用户不会认为它被冻结了。
private void readInvoices()
{
string InvoiceFile = txtInvoiceFile.Text;
//read in the text file and get all the invoices to copy
string[] Invoices = File.ReadAllLines(InvoiceFile);
//set the max val of the progress bar
progBar.Maximum = Invoices.Length;
try
{
//for every invoice
foreach (string invoice in Invoices)
{
//Set the source and destination directories
string sourceInvFolder = string.Format(@"{0}\{1}", txtSource.Text, invoice);
string destInvFolder = string.Format(@"{0}\{1}", txtDest.Text, invoice);
DirectoryInfo SourceDI = new DirectoryInfo(sourceInvFolder);
DirectoryInfo DestDI = new DirectoryInfo(destInvFolder);
//we know we have it in the CSV but does the directory actually exist?
//if so then let's process
if (Directory.Exists(SourceDI.FullName) == true)
{
//let's copy of the files
CopyAll(SourceDI, DestDI);
RenameFolder(sourceInvFolder);
}
//inc the progress bar
progBar.Increment(1);
}
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.Message);
}
finally
{
MessageBox.Show("Process Complete");
CleanUp();
}
}
【问题讨论】:
-
我的猜测是您在 UI 线程中完成所有这些工作。那肯定会冻结事情。不过,我们目前无法确定,因为我们无法确定您是如何调用您的方法的。一般来说,不要在 UI 线程中做繁重的工作。
标签: c# progress-bar