Hello,
I have the following example of AsyncCallback from a C# book which I wanted
to implement in my project:
//Class with AsyncDelegate
public class AsyncProcess
{
public AsyncProcess()
{
}
public delegate int AsynExecuteDele gate(string command);
public System.IAsyncRe sult BeginExecute(st ring command,
System.AsyncCal lback callBack)
{
//string temp;
AsynExecuteDele gate executeDelegate = new
AsynExecuteDele gate(this.Execu te);
return executeDelegate .BeginInvoke(co mmand, callBack, null);
}
public int Execute(string command)
{
System.Diagnost ics.Trace.Write Line(command);
//more code
return 1;
}
}
//Client BeginExecute Call and CallBack
private void button2_Click(o bject sender, System.EventArg s e)
{
AsyncProcess aPr = new AsyncProcess();
aPr.BeginExecut e("Do this", new AsyncCallback(C lientCallBack)) ;
}
private void ClientCallBack( System.IAsyncRe sult ar)
{
AsyncProcess.As ynExecuteDelega te del =
(AsyncProcess.A synExecuteDeleg ate)ar.AsyncSta te;
int result = del.EndInvoke(a r);
MessageBox.Show (result.ToStrin g());
}
Unfortunately this callback doesn't work, it behaves in a very strange way;
when I stepped through the callback, it looked like .EndInvoke would cause
ClientCallBack to be called again, and 2nd time around when it reached
..EndInvoke it would simply exit the function without returning any value.
Very Strange!
However this callback from code sample on MSDN
http://msdn.microsoft.com/library/de...mingsample.asp Seems to work:
using System.Runtime. Remoting.Messag ing;
.....
.....
private void ClientCallBack( System.IAsyncRe sult ar)
{
AsyncProcess.As ynExecuteDelega te del =
(AsyncProcess.A synExecuteDeleg ate)((AsyncResu lt)ar).AsyncDel egate;
int result = del.EndInvoke(a r);
MessageBox.Show (result.ToStrin g());
}
Can someone explain why that is. Why is it requiring AsyncDelegate wich is
part of System.Runtime. Remoting.Messag ing namespace, what does Remoting has
to do with AsyncCallbacks?
Thank you