开发者

Running method in the background to update UI

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜