I've drunk only one cofee, so what I'm about to tell you may be a complete
crap:)

What will happen if you just create your own class, which implements
IAsyncResult.
Let's name it AsyncResultWithState, and it will look like this (ignore
syntax, I'm using Outlook):

public class AsyncResultWithState : IAsyncResult
{
    public AsyncResultWithState(object state)
    {
        // this is the user-supplied state, when the BeginFoo(...., state)
was called
        this.state = state;
    }
    // IAsyncResult implementation is your job:)
   public object AdditionalState
   {
       get
       {
             return (additionalState);
       }
       get
       {
             additionalState = value;
       }
   }
   private object state, additionalState;
}

Now, let's say you have two classes, which implement the same async. pattern
for the Foo method.
String Foo(string whatever);
IAsyncResult BeginFoo(string whatever, AsyncCallback callback, object
state);
String EndFoo(IAsyncResult  result);

Now, in A's BeginFoo you want to delegate the call to B, e.g.

Public IAsyncResult BeginFoo(string whatever, AsyncCallback callback, object
state)
{
     IAsyncResult result = m_referenceToBacquiredAnyHow.BeginFoo(whatever,
callback, state);
     CustomAsyncResult result = (CustomAsyncResult)result;
     // you can easily pass any state here
     result.AdditionalState = DateTime.Now; // for example
     return (result);
}

The only problem I see is that B's BeginFoo may finish too fast and
"result.AdditionalState" may not be set for B to use it. However, you can
prevent this from
happening, by setting an event, on which B waits, e.g.

// modify the CustomAsyncResult to include an
AutoResetEvent AdditionaStateWasSet { get; }

// in A's BeginFoo

Public IAsyncResult BeginFoo(string whatever, AsyncCallback callback, object
state)
{
     IAsyncResult result = m_referenceToBacquiredAnyHow.BeginFoo(whatever,
callback, state);
     CustomAsyncResult result = (CustomAsyncResult)result;
     // you can easily pass any state here
     result.AdditionalState = DateTime.Now; // for example
     result.AdditionaStateWasSet.Set(); // signal the event
     return (result);
}

// in B.BeginFoo

Public IAsyncResult BeginFoo(string whatever, AsyncCallback callback, object
state)
{
        CustomAsyncResult result = CreateItSomehow();
        // launch a thread and pass it the result, so it can know when to
start processing
        Thread t = new Thread(new ThreadStart(new Worker(result)).Run);
        t.Start();
        return (result);
}

// in the thread
Public void Start()
{
        AutoResetEvent evtStartWorking =
this.theCustomAsyncResult.AdditionaStateWasSet;
        evtStartWorking.WaitOne(); // or whatever it was called:)
}

Do you get it?
Or my brain is turning into piss?:)

Cheers,
Stoyan

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to