Refactoring to extension method (2): Do not write (action) (delegate (){...}) Now

Source: Internet
Author: User

The dispatcher class of WPF is described as follows:

 

 
Public dispatcheroperation begininvoke (delegate method, Params object [] ARGs );

We have to do this when using:

 

 
Dispatcher. begininvoke (Action) Delegate () {// do something ...});

Don't you think it's ugly? It also needs to be forcibly converted to action.

But you said, its parameter is delegate. What should I do?

Oh, you forgot the extension method?

If we can change:

 

 
Dispatcher. begininvoke () =>{// do something ...});

In this way, it is much more convenient to use a lambda.

So we can write a dispatcher extension method:

 

Public static class dispatcherextensions {public static dispatcheroperation begininvoke (This dispatcher, action Action) {return dispatcher. begininvoke (action, new object [0]);} public static dispatcheroperation begininvoke (This dispatcher, Action action, dispatcherpriority priority) {return dispatcher. begininvoke (action, priority, new object [0]);} public static void invoke (This dispatcher, action Action) {dispatcher. invoke (action, new object [0]);} public static void invoke (This dispatcher, Action action, timespan timeout) {dispatcher. invoke (action, timeout, new object [0]);} public static void invoke (This dispatcher, Action action, dispatcherpriority priority) {dispatcher. invoke (action, priority, new object [0]);} public static void invoke (This dispatcher, Action action, timespan timeout, dispatcherpriority priority) {dispatcher. invoke (action, timeout, priority, new object [0]) ;}}

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.