Qosmo
Qosmo

Reputation: 1202

Asynchrony, doing simple but time consuming actions

I'm doing a WPF application that has to scan folders and files, and as you know, even if not much, it does take some time to iterate over many folders. Obviously you don't want to keep the application user waiting, not being able to do any other action (or just so the UI doesn't freeze). Where should I start to learn how to perform my folder scans asynchronously? If possible a simple reference!

Have a nice day!

Upvotes: 2

Views: 98

Answers (3)

user203570
user203570

Reputation:

Here is a nice article that shows using BackgroundWorker, including dispatching UI calls back to the UI thread.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564333

The easiest option is potentially to use a BackgroundWorker. This will automatically allow you to push your "work" into the background, and report back progress and completion to the WPF thread without manually synchronizing via the Dispatcher.

Another option would be to use the TPL via a Task with continuations. For example, when you start your method, you can do:

// Disable your UI...

// Start your "work"
var task = Task.Factory.StartNew( () =>
{
    // Do your processing here...
});

task.ContinueWith( t =>
{
    // Report results and re-enable UI here...

}, TaskScheduler.FromCurrentSynchronizationContext());

Upvotes: 2

Jollymorphic
Jollymorphic

Reputation: 3530

Probably the MSDN page on the .NET framekwork's threading features is a good place to start.

Upvotes: 2

Related Questions