Question

I use to browse this forum to find answers but this time I just couldn't figure out anything that would make me completely understand the following question.

I've got this program :

IAsyncResult asrr;
        DELG d = (o) =>
        {
            short i = 10;
            string msg = (string)o;
            while (i > 0)
            {
                Console.WriteLine("Work - > {0}::{1}", i, msg);
                System.Threading.Thread.Sleep(1000);
                i--;
            }
        };
        asrr = d.BeginInvoke(((object)("T1")), (asr) =>
        {
            DELG dlg = (DELG)((System.Runtime.Remoting.Messaging.AsyncResult)asr).AsyncDelegate;
            dlg.EndInvoke(asr);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("callback");
            Console.ForegroundColor = ConsoleColor.Gray;
        }, d);

Then I think I understood what's happening here : We have a delegate that is asynchronously called using asrr. We give the delegate 'd' several args : T1, which will be displayed in the delegate, a callback methode (I'm using a lambda methode because this was requiered in this exercice) and d, the delegate.

Well I don't really get it... I understand we send T1 but where comes "(asr)" from ? I don't get anything that is happening after the begininvoke nor what does :

DELG dlg = (DELG)((System.Runtime.Remoting.Messaging.AsyncResult)asr).AsyncDelegate;

Here again I can see the asr but don't understand it.

I've found this link : Delegates and Events But even though it helped me, I still don't get the whole of it. Would you please provide me some information about it ?

Thanks a lot to the incoming answers.

Was it helpful?

Solution

To add further to others' answers.

This is Asynchronous call.

As per MSDN ,

The BeginInvoke method initiates the asynchronous call. It has the same parameters as the method that you want to execute asynchronously, plus two additional optional parameters. The first parameter is an AsyncCallback delegate that references a method to be called when the asynchronous call completes. The second parameter is a user-defined object that passes information into the callback method. BeginInvoke returns immediately and does not wait for the asynchronous call to complete. BeginInvoke returns an IAsyncResult, which can be used to monitor the progress of the asynchronous call.

The EndInvoke method retrieves the results of the asynchronous call. It can be called any time after BeginInvoke. If the asynchronous call has not completed, EndInvoke blocks the calling thread until it completes. The parameters of EndInvoke include the out and ref parameters (<Out> ByRef and ByRef in Visual Basic) of the method that you want to execute asynchronously, plus the IAsyncResult returned by BeginInvoke

Look at the link for further info.

OTHER TIPS

You're declaring the asr in the BeginInvoke call - the part that has (asr) => is the declaration. The type is inferred.

asr is the input parameter for your anonymous delegate. see http://msdn.microsoft.com/en-us/library/vstudio/bb397687.aspx for more information.

the delegate has a signature with one input parameter. you declare the input parameter to be referred as asr in the function block between the braces.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top