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