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來同步事件,你都在做錯事。 –