WordPress database error: [INSERT, UPDATE command denied to user '51213-2'@'10.10.20.169' for table 'wp_options']INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`) VALUES ('_transient_doing_cron', '1716245817.7477490901947021484375', 'yes') ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)
<\/a>This is the third part of the serie on the WPF 4.5 new features<\/a>.<\/p>\n The Dispatcher class is maybe one of the most used in WPF when you start doing thing asyncronously. It is the only way to update UI controls from an another thread. <\/p>\n Even if it was easy to use, the WPF teams added 13 methods which will ease up this. Especially with the new await keyword. In this post we will discover these new methods. <\/p>\n There is some overload which takes as a param a Func delegate. Where in prior versions, the available methods on the Dispatcher were not able to return something (except void) it is now possible. <\/strong><\/p>\n The new methods of this kind are :<\/p>\n Prior to WPF 4.5, to return something, this code should have been written: object returnedOject = null;<\/p>\n \/\/A mock action to be abble to return the object \/\/Actually invoke the method \/\/Here I can use the returned object Now, you can write a more maintanable code by using this one: \/\/This method can now live alone ! You surely are aware of the new-born ‘await’ keyword. Here is the new methods which are await-ready:<\/p>\n These method returns objects of the DispatcherOperation\/DispatcherOperation Also, you can do some synchronisation and wait for a Dispatcher operation to be finished by using the DispatcherOperationWait method on the task. This is an extension method of the TaskExtensions class (available on System.Windows.Threading). dispatcherOperation.Task.Wait(); There is a disclaimer\/warning on this last one “Calling Task.Wait will result in a deadlock if the operation is queued on a calling thread. For more information about using a Task to perform asynchronous operations, see Task Parallelism (Task Parallel Library).”<\/em><\/p>\n
\n<\/strong><\/p>\nThe new ‘classic’ methods<\/h3>\n
\n
\n[csharp]\/\/The function which returns something (here of type object)
\nFunc<object> myReturningObjectFunction = () =>
\n{\/\/For example only !
\n return new object();
\n};<\/p>\n
\nAction mockAction = () => { returnedOject = myReturningObjectFunction(); };<\/p>\n
\nDispatcher.CurrentDispatcher.Invoke(mockAction, null);<\/p>\n
\n[\/csharp]<\/p>\n
\n[csharp]public void CallingMethod()
\n{
\n object returnedOject = Dispatcher.CurrentDispatcher
\n .Invoke(MyReturningObjectFunction, null);
\n}<\/p>\n
\nprivate object MyReturningObjectFunction()
\n{
\n \/\/For example only !
\n return new object();
\n}
\n[\/csharp]<\/p>\nAwait-ready !<\/h3>\n
\nThe WPF team is too and the Dispatcher is now await-ready ! <\/strong><\/p>\n\n
\nYou can then use the await keyword on it or on its ‘Task’ property.
\nHere is an example:
\n[csharp]public async void CallingMethod()
\n{
\n await Dispatcher.CurrentDispatcher.InvokeAsync(MyReturningObjectFunction);
\n}
\n[\/csharp]<\/p>\n
\n[csharp]DispatcherOperation<object> dispatcherOperation =
\n Dispatcher.CurrentDispatcher.InvokeAsync(MyReturningObjectFunction);<\/p>\n
\n[\/csharp]<\/p>\nCancellation<\/h3>\n