0
似乎是一个微不足道的问题。调用Directory.EnumerateFiles的BackgroundWorker在枚举完成之前完成
我有以下代码片段。 EnumerateFiles实际完成之前似乎完成的事件触发。例如,如果像C:\users\<user>\desktop
这样的路径的tb_source.Text ...被设置为具有一百个左右项目的文件夹,则所有作品和reference.throttler_numer_of_files
都被正确填充。
但是,看起来如果我设置这样说,C:\users\<user>
它永远不会有机会找到所有事情,然后再继续完成事件。试图计算文件时可能遇到异常?
Suggesetions?
// 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();
检查错误性质http://msdn.microsoft.com/de-de/library/system.componentmodel。 runworkercompletedeventargs(v = vs.80).aspx – usr
无论何时你必须调用Thread.Sleep来同步事件,你都在做错事。 –