G
Guest
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 AsynExecuteDelegate(string command);
public System.IAsyncResult BeginExecute(string command,
System.AsyncCallback callBack)
{
//string temp;
AsynExecuteDelegate executeDelegate = new
AsynExecuteDelegate(this.Execute);
return executeDelegate.BeginInvoke(command, callBack, null);
}
public int Execute(string command)
{
System.Diagnostics.Trace.WriteLine(command);
//more code
return 1;
}
}
//Client BeginExecute Call and CallBack
private void button2_Click(object sender, System.EventArgs e)
{
AsyncProcess aPr = new AsyncProcess();
aPr.BeginExecute("Do this", new AsyncCallback(ClientCallBack));
}
private void ClientCallBack(System.IAsyncResult ar)
{
AsyncProcess.AsynExecuteDelegate del =
(AsyncProcess.AsynExecuteDelegate)ar.AsyncState;
int result = del.EndInvoke(ar);
MessageBox.Show(result.ToString());
}
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/d...conasynchronousdelegatesprogrammingsample.asp Seems to work:
using System.Runtime.Remoting.Messaging;
.....
.....
private void ClientCallBack(System.IAsyncResult ar)
{
AsyncProcess.AsynExecuteDelegate del =
(AsyncProcess.AsynExecuteDelegate)((AsyncResult)ar).AsyncDelegate;
int result = del.EndInvoke(ar);
MessageBox.Show(result.ToString());
}
Can someone explain why that is. Why is it requiring AsyncDelegate wich is
part of System.Runtime.Remoting.Messaging namespace, what does Remoting has
to do with AsyncCallbacks?
Thank you
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 AsynExecuteDelegate(string command);
public System.IAsyncResult BeginExecute(string command,
System.AsyncCallback callBack)
{
//string temp;
AsynExecuteDelegate executeDelegate = new
AsynExecuteDelegate(this.Execute);
return executeDelegate.BeginInvoke(command, callBack, null);
}
public int Execute(string command)
{
System.Diagnostics.Trace.WriteLine(command);
//more code
return 1;
}
}
//Client BeginExecute Call and CallBack
private void button2_Click(object sender, System.EventArgs e)
{
AsyncProcess aPr = new AsyncProcess();
aPr.BeginExecute("Do this", new AsyncCallback(ClientCallBack));
}
private void ClientCallBack(System.IAsyncResult ar)
{
AsyncProcess.AsynExecuteDelegate del =
(AsyncProcess.AsynExecuteDelegate)ar.AsyncState;
int result = del.EndInvoke(ar);
MessageBox.Show(result.ToString());
}
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/d...conasynchronousdelegatesprogrammingsample.asp Seems to work:
using System.Runtime.Remoting.Messaging;
.....
.....
private void ClientCallBack(System.IAsyncResult ar)
{
AsyncProcess.AsynExecuteDelegate del =
(AsyncProcess.AsynExecuteDelegate)((AsyncResult)ar).AsyncDelegate;
int result = del.EndInvoke(ar);
MessageBox.Show(result.ToString());
}
Can someone explain why that is. Why is it requiring AsyncDelegate wich is
part of System.Runtime.Remoting.Messaging namespace, what does Remoting has
to do with AsyncCallbacks?
Thank you