Doing the Threading stuff is pretty easy - a Background worker will handle that:
BackgroundWorker work = new BackgroundWorker();
work.DoWork += new DoWorkEventHandler(work_DoWork);
work.ProgressChanged += new ProgressChangedEventHandler(work_ProgressChanged);
work.WorkerReportsProgress = true;
work.RunWorkerCompleted += new RunWorkerCompletedEventHandler(work_RunWorkerCompleted);
work.RunWorkerAsync(files);
...
void work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
butSync.Enabled = true;
}
void work_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string mess = e.UserState as string;
Console.WriteLine(mess);
}
void work_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker work = sender as BackgroundWorker;
}
The complication comes when you are adding the actual TreeNodes, because the background worker can't access UI components (such as the TreeView) directly without causing a cross threading exception. You could use Invoke to move that code onto your main thread, but then you are throwing events around like a loon in an application such as this.
The method I used last time I did this was to create the folders as a list in the background worker and then use the Worker.ReportProgess to pass the list and the node to which it applies back up to the UI thread for it to add - because each event does a limited set of work it doesn't impact the UI so badly and the user gets a sensible update each time.