The ininvoke and BeginInvoke traps are used to delegate asynchronous calls.

Source: Internet
Author: User

The ininvoke and BeginInvoke traps are used to delegate asynchronous calls.

This trap comes from a requirement that the data needs to be processed asynchronously in the background. After processing, the event that is triggered to be processed is probably written as follows:

EmployeeCollection data = new EmployeeCollection();data.Loaded += data_Loaded;Action<EmployeeCollection> action = (d) => {    DalHelper.Fill(data);    data.RaiseEventLoaded();};action.BeginInvoke(data, null, null);

The code is quite simple, and the traps are also in it. Suppose DalHelper. fill (data) throws an exception. raiseEventLoaded () won't be executed and depends on data. the Loaded Event code will not be executed either. This is a bug. You should add a try... catch statement, or call the delegate EndInvoke method somewhere to handle possible exceptions during execution.

 

For such a simple requirement, add try... the EndInvoke of the catch or call delegate is too complicated. If the execution fails, the exception is thrown out, even if the current process is finished. Based on the principle of one write and multiple use, a dedicated help class is specially designed for dedicated asynchronous calls of such delegation. The help class code is as follows:

public class EventHelper {   public static void UnsafeBeginInvoke(Delegate del,params object[] args){      AsyncFire asyncFire = InvokeDelegate;      asyncFire.BeginInvoke(del, args, ThrowCallback, asyncFire);   }         delegate void AsyncFire(Delegate del,object[] args);    static void InvokeDelegate(Delegate del,object[] args){        del.DynamicInvoke(args);    }   static void ThrowCallback(IAsyncResult ar) {        AsyncFire asyncFire = ar.AsyncState as AsyncFire;       asyncFire.EndInvoke(ar);   }}

The core implementation is to encapsulate the call of the Delegate, call it in another delegate, and then use EndInvoke to release possible exceptions for another delegate, in this way, you can find the exceptions that occur when you call BeginInvoke and then delegate the execution.After this modification, the code just now can be called as follows:

EmployeeCollection data = new EmployeeCollection();data.Loaded += data_Loaded;Action<EmployeeCollection> action = (d) => {    DalHelper.Fill(data);    data.RaiseEventLoaded();};EventHelper.UnsafeBeginInvoke(action, data);

The Code is as simple as the original design. If an exception occurs in the delegate, this error can also be found, rather than being hidden.

 

In addition, the implementation just now is not type-safe, and type-security can be solved through heavy load. The example is as follows:

Public class EventHelper {public static void UnsafeBeginInvoke (Delegate del, params object [] args) {AsyncFire asyncFire = InvokeDelegate; asyncFire. beginInvoke (del, args, ThrowCallback, asyncFire);} delegate void AsyncFire (Delegate del, object [] args); static void InvokeDelegate (Delegate del, object [] args) {del. dynamicInvoke (args);} static void ThrowCallback (IAsyncResult ar) {AsyncFire asyncFire = ar. asyncState as AsyncFire; asyncFire. endInvoke (ar) ;}# region add type-safe delegate public static void BeginInvoke (Action del) {UnsafeBeginInvoke (del);} public static void BeginInvoke <T, u> (Action <T, U> del, T t, U) {UnsafeBeginInvoke (del, t, u);} public static void BeginInvoke <T, u, v> (Action <T, U> del, T t, U u, V) {UnsafeBeginInvoke (del, t, u, v );} # adding a type-safe delegate to endregion}View Code

You can add the implementation of type security as needed.

Related Article

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.