473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

AsyncCallback Explain why my code is not working

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/de...mingsample.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
Nov 17 '05 #1
3 9399
Well, According to
http://msdn.microsoft.com/library/de...gmessaging.asp

<quoute>
System.Runtime.Remoting.Messaging namespace are the AsyncResult class....

The AsyncResult class stores and returns the results of an asynchronous
method call. AsyncResult instances contain the return value, call status, the
delegate used for the call, and the other information about the asynchronous
method call.
</quoute>

It's just strange that they put in Remoting.Messaging namespace .

So now, if anyone has advanced knowledge about async callbacks, can someone
expalin why example from a book wont work while msdn.
To summarazie the difference is 1 line of code:

MSDN Working code:
AsyncProcess.AsynExecuteDelegate del =
(AsyncProcess.AsynExecuteDelegate)((AsyncResult)ar ).AsyncDelegate;

Book's Not working code:
AsyncProcess.AsynExecuteDelegate del =
(AsyncProcess.AsynExecuteDelegate)ar.AsyncState;

I will stop taling to myself now :-)

Thanks

"Lenn" wrote:
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/de...mingsample.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

Nov 17 '05 #2
Lenn,

You need to use the same delegate instance that made the call to
BeginInvoke to call EndInvoke. If you don't, then the results will not be
correct.

Also, your implementation of BeginInvoke should include a parameter for
state to be passed to it (you pass null, and omit the parameter). This
would fall in line with the pattern that has been established by MS.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lenn" <Le**@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
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/de...mingsample.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

Nov 17 '05 #3


"Nicholas Paldino [.NET/C# MVP]" wrote:
Lenn,

You need to use the same delegate instance that made the call to
BeginInvoke to call EndInvoke. If you don't, then the results will not be
correct.

Also, your implementation of BeginInvoke should include a parameter for
state to be passed to it (you pass null, and omit the parameter). This
would fall in line with the pattern that has been established by MS.

Hope this helps.


Yes, it actually does. I see now, that I didn't follow book's example all
the way, I should've passed the same delegate instance that made the call to
BeginInvoke as a state object, it should've been:

return executeDelegate.BeginInvoke(command, callBack, executeDelegate);

Now original Callback works. MSDN pattern is very interesting... They derive
original .BeginInvoke delegate from AsyncResult cllass. There're so many
different ways to do the same thing in .NET, and it's not clear what's the
most efficient one, I wish MSDN provided more documentation on this.
Thanks for your input!
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Craig Vermeer | last post by:
Hi All, I noticed an interesting issue the other day, and thought someone here might be able to shed some light on things. I have a situation where I'm using asynchronous delegates, and an...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
11
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and...
1
by: Lenn | last post by:
Hello, Could you provide specific examples, when one would want to use AsyncCallback as opposed to just subscribing to events or passing a delegate to a function? I realize that AsyncCallback is...
1
by: Lenn | last post by:
Hi, I am using .BeginInvoke to make an asynchronous call to a function, and passing in a AsyncCallback, so client gets notification when method completes, relevant code is provided below: ...
2
by: Jared | last post by:
Hello all, I have a web service that I am trying to retrieve data from asynchronously. According to the MSDN documentation...
6
by: James Wong | last post by:
Hi, Can WinCE & Pocket PC support the "AsyncCallback"?
1
by: Alper AKCAYOZ | last post by:
Hello, I have developped asynchronous socket communication with blocking Socket commands (accept, connect, send, receive) by using threads on Windows .NET Forms. It is working properly. Now I...
2
by: Steven Blair | last post by:
Hi, I am using an AsyncCallback to receive a TcpClient on incoming connections. The code below works fine until I start the application and no connection is received within the time limit. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.