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
}));
});
精彩评论