0

A trivial issue it seems.

I have the following code snippet. It seems that the completed event fires before EnumerateFiles actually finishes. For example, if the tb_source.Text ... which is a path like C:\users\<user>\desktop is set to a folder with say a hundred or so items, all works and reference.throttler_numer_of_files is correctly populated.

It looks though that if I set that to say, C:\users\<user> it never gets a chance to find everything before moving on to the completed event. Perhaps it encounters an exception when trying to count the files?

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();
Patrick Alexson
  • 153
  • 1
  • 4
  • 13
  • 1
    Inspect the error property http://msdn.microsoft.com/de-de/library/system.componentmodel.runworkercompletedeventargs(v=vs.80).aspx – usr Jul 17 '13 at 20:51
  • 2
    Any time you have to call `Thread.Sleep` to synchronize events, you're doing something wrong. – Lasse V. Karlsen Jul 17 '13 at 20:55

1 Answers1

0

Turns out the issue was that there was an exception (access denied) error during EnumerateFiles. I'm able to catch the exception but now I must determine a good way I can continue the enumeration, making it move on to the next file/directory.

Edit - for future reference, details here were helpful on how to do the above: Directory.EnumerateFiles => UnauthorizedAccessException

Community
  • 1
  • 1
Patrick Alexson
  • 153
  • 1
  • 4
  • 13