473,796 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

<quoute>
System.Runtime. Remoting.Messag ing 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.Messag ing 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.As ynExecuteDelega te del =
(AsyncProcess.A synExecuteDeleg ate)((AsyncResu lt)ar).AsyncDel egate;

Book's Not working code:
AsyncProcess.As ynExecuteDelega te del =
(AsyncProcess.A synExecuteDeleg ate)ar.AsyncSta te;

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 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

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.co m

"Lenn" <Le**@discussio ns.microsoft.co m> wrote in message
news:59******** *************** ***********@mic rosoft.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 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

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(co mmand, 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
2431
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 exception is getting thrown from within the AsyncCallback on the delegate. For some reason, this causes the AsyncCallback delegate to be executed twice. Now, I understand how to stop the situation from happening. However, what I didn't know was...
1
5228
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 peer. I have no problem connecting the peers and streaming Network Streams. When the incoming data is finished recieving, I act upon it. This works great as long as all of the code is inside my form. I want to build the networking code into a...
11
3790
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 noticed that if I did this, it worked (please ignore the lack of try ... catch blocks, because I cut down the code to be more brief - I have a try...catch surround the AcquireWriterLock method): ---------- myLock.AcquireWriterLock(1000);...
1
1522
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 a delegate type, but when it's absolutely necessary to use it. Thanks
1
8304
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: AsyncCallback callback = new AsyncCallback(this.NotifyIt); FtpClient ftpC = new FtpClient(FtpServer, FtpUserName, FtpPassword); private void button4_Click_1(object sender, System.EventArgs e) {
2
2130
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 (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtskCallingWebServiceAsynchronously.htm) I should be able to call a webservice asynchronously using a delegate function. The callback seems to work, but, when I assign the value to an existing datagrid the program hangs. I don't quite understand, what do I...
6
2464
by: James Wong | last post by:
Hi, Can WinCE & Pocket PC support the "AsyncCallback"?
1
6682
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 want to code the similar program with Asynchronous Socket commands of .NET using Managed C++ on Windows .NET Forms. My problem is with delegates. I have to use "static" methods as parameter in delegate constructor like below:...
2
4914
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 could easily try / catch the code causing the problem but I would like to understand why this problem is happening. I was unable to find any field on the TcpListener that would indicate the Listenign socket was stopped.
0
9679
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.