似乎是一个微不足道的问题。
我有以下代码片段。似乎已完成的事件在 EnumerateFiles 实际完成之前触发。例如,如果 tb_source.Text ...(类似路径C:\users\<user>\desktop
)设置为包含一百左右项目的文件夹,则一切正常并reference.throttler_numer_of_files
正确填充。
看起来,如果我这样说,C:\users\<user>
在继续完成的活动之前,它永远没有机会找到所有东西。也许它在尝试计算文件时遇到异常?
建议?
// count number of files within source directory (and within subdirectories)
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(
delegate(object o, DoWorkEventArgs args)
{
Thread.Sleep(2000); // give the current UI a moment to load
reference.throttler_number_of_files = Directory.EnumerateFiles(tb_source.Text, "*.*", SearchOption.AllDirectories).Count();
}
);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
delegate(object o, RunWorkerCompletedEventArgs args)
{
// success, clear to launch!
form_throttler_copy throttler_copy = new form_throttler_copy();
throttler_copy.Show();
this.Dispose();
}
);
label_status.Text = "Creating manifest of files... please wait";
picturebox_loading.Visible = true;
bw.RunWorkerAsync();