cool hit counter [C#] Callback method does not pass object parameter to get delegate instance_Intefrankly

[C#] Callback method does not pass object parameter to get delegate instance


There will almost always be a need in the callback method to get a delegate instance, which in turn calls EndInvoke to get the return value of the asynchronous execution. In the related articles I've read, the way to get a delegate instance is almost always the same, which is to pass the delegate instance to the last parameter (of type object) of BeginInvoke, and then pass it in the callback method via ar. AsyncState gets it, and even in the Advanced Programming in C# book, it's specifically talked about how this last object parameter is best passed into the delegate instance itself so that it can be fetched in the callback method. It wasn't until I read Zhang Ziyang's . After the Beauty of the Net book (which seems like an advertisement, but it is, so I won't shy away from it), I learned a new trick to get delegate examples directly from ar without passing references. as follows.

    ......
    DemoDelegate del = DemoMethod;
del. BeginInvoke(CallBackMethod, del);// pass the delegate instance itself to the last argument
    
    Console.ReadKey();
}

 // Callback methods
public static void CallBackMethod(IAsyncResult ar)
{
     object a = ar. AsyncState;  /Common methods
     object b = ((AsyncResult)ar). AsyncDelegate;  /New approach
    Console.WriteLine(a != null && a == b); //exportstrue, It shows that the new method is reliable
}

public delegate void DemoDelegate();

public static void DemoMethod() { }

That is, after converting ar strong to the AsyncResult type, you can get the delegate instance through the property AsyncDelegate. This frees up that original object parameter for other uses, so it makes sense to me to master this new method. Where this AsyncResult class is necessary to explain, in.

Namespace.System.Runtime.Remoting.Messaging Procedural set.mscorlib.dll Documentation.http://msdn.microsoft.com/zh-cn/library/System.Runtime.Remoting.Messaging.AsyncResult(v=vs.80).aspx

As I understand it this AsyncResult class should be one of the more public base classes, but why MS has grouped it in such an obscure namespace is unknown, and probably because of that the method of getting a delegate instance via AsyncResult.AsyncDelegate is not common.


Recommended>>
1、Error while buildingdeployingprojectdictqtkitDesktopQt5100Mi after running a program after a new installation of QtCreator
2、The field of gaming AI research and application of robotics
3、Matlab Quantitative Investing Support Vector Machine Timing Strategy
4、How do you use classes in reference data types in Java
5、From theory to practice an article detailing the three main algorithms of AI recommender systems

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号