paradox
paradox

Reputation: 1276

Running method in the background to update UI

How do I run a method in the background for c# wpf? It is a packet sniffing method which will update the UI whenever new data is received, do I have to use dispatcher.invoke?

Upvotes: 3

Views: 460

Answers (2)

Matt
Matt

Reputation: 4334

There are a lot of ways to do this in WPF, but here's one very simple way using Task to do the work on another thread and then dispatching the UI updating back to the main thread:

Task.Factory.StartNew(() =>
{
    // some work (packet sniffing)

    // update UI
    this.Dispatcher.BeginInvoke(new Action(() =>
    {
        // update my controls here
    }));
});

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300529

You could use the Dispatcher or the BackgroundWorker: See Build More Responsive Apps With The Dispatcher

Upvotes: 2

Related Questions