public class AsyncDemo
{
// Use in asynchronous methods
private delegate string runDelegate();
private string m_Name;
private runDelegate m_Delegate;
public AsyncDemo(string name)
{
m_Name = name;
m_Delegate = new runDelegate(Run);
}
/**//// <summary>
/// Synchronous method
/// </summary>
/// <returns></returns>
public string Run()
{
return "My name is " + m_Name;
}
/**//// <summary>
/// Asynchronous begin method
/// </summary>
/// <param name="callBack"></param>
/// <param name="stateObject"></param>
/// <returns></returns>
public IAsyncResult BeginRun(AsyncCallback callBack, Object stateObject)
{
try
{
return m_Delegate.BeginInvoke(callBack, stateObject);
}
catch(Exception e)
{
// Hide inside method invoking stack
throw e;
}
}
/**//// <summary>
/// Asynchronous end method
/// </summary>
/// <param name="ar"></param>
/// <returns></returns>
public string EndRun(IAsyncResult ar)
{
if (ar == null)
throw new NullReferenceException("Arggument ar can't be null");
try
{
return m_Delegate.EndInvoke(ar);
}
catch (Exception e)
{
// Hide inside method invoking stack
throw e;
}
}
}