You only need DispatcherHelper when you want to make changes to the components of your UI thread, starting with code that runs in another thread. For instance. in a Silverlight application, you call a web service to retrieve some data asynchronously and now want to tell Ui that the data is present through the OnNotifyPropertyChanged event.
You must initialize the DispatcherHelper . In Silverlight, you do this in Application_Startup :
//initialize Dispatch helper private void Application_Startup( object sender, StartupEventArgs e) { RootVisual = new MainPage(); DispatcherHelper.Initialize(); }
In WPF, initialization is done in the static constructor of the App class:
static App() { DispatcherHelper.Initialize(); }
Then in your case, handling the completion of your asnc call, use the following code to call RaisePropertyChanged in the user interface thread:
DispatcherHelper.CheckBeginInvokeOnUI( () => RaisePropertyChanged(PowerStatePropertyName) );
DispatcherHelper.BeginInvokeOnUl expects Action , so you can use any code here, just use
DispatcherHelper.CheckBeginInvokeOnUI( () => { } );
to perform more complex tasks.
source share