using AsyncResult to obtain results from delegates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    using AsyncResult to obtain results from delegates

    Below is the example that I mercilessly copied from Jon's article. In
    this example, I do not understand how his AsyncResult was able to
    retrieve delegates. In other words, using AsyncResult delegateResult
    = (AsyncResult) result --will it always return the active/completed
    delegates in loaded in CLR?

    Thanks


    using System;
    using System.Threadin g;
    using System.Runtime. Remoting.Messag ing;

    delegate int SampleDelegate( string data);

    class AsyncDelegateEx ample2
    {
    static void Main()
    {
    SampleDelegate counter = new SampleDelegate( CountCharacters );
    SampleDelegate parser = new SampleDelegate( Parse);

    AsyncCallback callback = new AsyncCallback (DisplayResult) ;

    counter.BeginIn voke ("hello", callback, "Counter returned
    {0}");
    parser.BeginInv oke ("10", callback, "Parser returned {0}");

    Console.WriteLi ne ("Main thread continuing");

    Thread.Sleep (3000);
    Console.WriteLi ne ("Done");
    }

    static void DisplayResult(I AsyncResult result)
    {
    string format = (string) result.AsyncSta te;
    AsyncResult delegateResult = (AsyncResult) result;
    SampleDelegate delegateInstanc e =
    (SampleDelegate )delegateResult .AsyncDelegate;

    Console.WriteLi ne (format,
    delegateInstanc e.EndInvoke(res ult));
    }

    static int CountCharacters (string text)
    {
    Thread.Sleep (2000);
    Console.WriteLi ne ("Counting characters in {0}", text);
    return text.Length;
    }

    static int Parse (string text)
    {
    Thread.Sleep (100);
    Console.WriteLi ne ("Parsing text {0}", text);
    return int.Parse(text) ;
    }
    }
  • Peter Duniho

    #2
    Re: using AsyncResult to obtain results from delegates

    On Sun, 05 Oct 2008 14:04:43 -0700, puzzlecracker <ironsel2000@gm ail.com>
    wrote:
    Below is the example that I mercilessly copied from Jon's article. In
    this example, I do not understand how his AsyncResult was able to
    retrieve delegates. In other words, using AsyncResult delegateResult
    = (AsyncResult) result --will it always return the active/completed
    delegates in loaded in CLR?
    The AsyncResult isn't special. It's just a class that stores state
    related to the asynchronous invocations of the delegates used in the
    examples. In particular, the AsyncDelegate simply returns the reference
    to the delegate that was used in the call to BeginInvoke() in the first
    place.

    Because that delegate instance is the one that created the AsyncResult
    instance, it's trivial for that reference to be included in the
    AsyncResult instance.

    Note, by the way, that there's no such thing as "the active/completed
    delegates in loaded in CLR". Whatever you meant by that, a particular
    delegate is neither active nor inactive, nor is it completed or
    incomplete. You can call BeginInvoke() multiple times on the same
    delegate. It's not the delegate that's "active" or "completed" , but
    rather the specific asynchronous invocation of the delegate.

    For example, if you had this code:

    void Method()
    {
    Action action = delegate { Thread.Sleep(50 00); };

    action.BeginInv oke(Callback, "action 1");
    Thread.Sleep(25 00);
    action.BeginInv oke(Callback, "action 2");
    }

    void Callback(IAsync Result result)
    {
    Console.WriteLi ne("finished delegate {0}", result.AsyncSta te);
    }

    Then after Callback is called the first time, would you consider the
    delegate "active" or would you consider it "completed" ? The delegate is
    "completed" in the sense that the callback was called, but it's "active"
    in the sense that there's still an async invocation of the delegate that
    hasn't finished yet.

    In other words, the words "active" and "completed" don't describe the
    delegate at all.

    Pete

    Comment

    Working...