Calling a method using delegates asynchronously
using System.Threading;
using System.Runtime.Remoting.Messaging;
private void ActualAsyncMethodCalled()
{
}
private void AsyncMethodCallBack(IAsyncResult result)
{
lock (result)
{
AsyncResult asyncResult = (AsyncResult) result;
if (asyncResult.AsyncDelegate != null)
{
Action sourceAction = (Action)asyncResult.AsyncDelegate;
sourceAction.EndInvoke(result);
}
}
}
public void Main()
{
Action asyncAction = ActualAsyncMethodCalled;
asyncAction.BeginInvoke(new AsyncCallback(AsyncMethodCallBack), null);
}
Using Action<T> if the method to be called takes one parameter, always call end invoke when you call begin invoke. Only in windows forms Control.BeginInvoke, this rule is explicitly not required as per MSDN.
using System.Runtime.Remoting.Messaging;
private void ActualAsyncMethodCalled()
{
}
private void AsyncMethodCallBack(IAsyncResult result)
{
lock (result)
{
AsyncResult asyncResult = (AsyncResult) result;
if (asyncResult.AsyncDelegate != null)
{
Action sourceAction = (Action)asyncResult.AsyncDelegate;
sourceAction.EndInvoke(result);
}
}
}
public void Main()
{
Action asyncAction = ActualAsyncMethodCalled;
asyncAction.BeginInvoke(new AsyncCallback(AsyncMethodCallBack), null);
}
Using Action<T> if the method to be called takes one parameter, always call end invoke when you call begin invoke. Only in windows forms Control.BeginInvoke, this rule is explicitly not required as per MSDN.
Comments
Post a Comment