How to use IAsyncResult

2.1k Views Asked by At

i have a method with the following signature that belongs to a class.

public virtual IAsyncResult DoSomething(CustomOptions options);

I am trying to figure out how exactly do i call the callback.I cannot find anywhere the event on which to give a callback method.

2

There are 2 best solutions below

0
On

To understand how to use IAsyncResult, you should understand where it would be use. It's normally used for asynchronous call. The most common usage is delegate asynchronous call. In that case, IAsyncResult is a receipt, it is used as an "Infomation carrier", and provides a synchronization object in order to abort the thread when asynchronous operation completes.

Usually you don't need to create an IAsyncResult. IAsyncResult is just a way to implement receipt function. You may not make it so complicated. Just transmit a simple struct to carry infomations you need.

like:

/// <summary>
/// This is a simplified IAsyncResult
/// </summary>
public class Receipt
{
    /// <summary>
    /// Name
    /// </summary>
    public String Name
    {
        get;
        set;
    }

    /// <summary>
    /// Age
    /// </summary>
    public Byte Age
    {
        get;
        set;
    }

    public String OperationResultText
    {
        get;
        set;
    }
}

public class Test
{
    public delegate void Async_OperationCallbackHandler(Receipt r);

    public void MainEntry()
    {
        Thread tmpThread = new Thread(()=>
        {
            Async_Operation("ASDF", 20, Async_Operation_Callback);
        });

        tmpThread.Start();
    }

    public void Async_Operation(String name, Byte age, Async_OperationCallbackHandler callback)
    {
        //Do something with "name" and "age" and get result...
        String result = "OK...";

        Receipt r = new Receipt()
        {
                Age = age,
                Name = name,
                OperationResultText = result
        };

        callback(r);
    }

    internal void Async_Operation_Callback(Receipt r)
    {
        Console.WriteLine("Name = " + r.Name + ", Age = " + r.Age + ", Operation result: " + r.OperationResultText);
    }
}

Of course, I did not consider synchronization. But .NET Framework has taken into it. So determine the contents of receipt according to your needs, not need to use IAsyncResult.

See:

Calling Synchronous Methods Asynchronously

IAsyncResult Interface

0
On

This is copied pretty much from MSDN:

    // Initiate the asychronous call.
    IAsyncResult result = caller.BeginInvoke(3000, 
        out threadId, null, null);

    Thread.Sleep(0);
    Console.WriteLine("Main thread {0} does some work.",
        Thread.CurrentThread.ManagedThreadId);

    // Wait for the WaitHandle to become signaled.
    result.AsyncWaitHandle.WaitOne();

    // Perform additional processing here. 
    // Call EndInvoke to retrieve the results. 
    string returnValue = caller.EndInvoke(out threadId, result);

If the method is your own, you might want to try returning a Task, which will have a ContinueWith method, that takes a block of code (Another Task) as a callback, which will run once the Task has completed.