开发者

Running method in the background to update UI

开发者 https://www.devze.com 2023-03-19 16:05 出处:网络
How do I run a method i开发者_开发百科n 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?You could u

How do I run a method i开发者_开发百科n 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?


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


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
    }));
});
0

精彩评论

暂无评论...
验证码 换一张
取 消