|
| Asynchronous Delegate w/ Callback Parameters |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Michael C |
| GOOD ANSWER |
Hi all,
Is there an easy way to get the parameters of an asynchronous delegate call from the callback function? Here's an example of what I'm trying to do:
private delegate ArrayList AsyncDelegate (string server);
private void GetServerInfo (string server) { AsyncDelegate ad = new AsyncDelegate(GetServerArray); IAsyncResult iar = ad.BeginInvoke(server, new AsyncCallback(EndGetServerInfo), ad); }
private void EndGetServerInfo(IAsyncResult iar) { AsyncDelegate ad = (AsyncDelegate)iar.AsyncState; ArrayList a = ad.EndInvoke(iar); }
private ArrayList GetServerArray(string server) { ArrayList a = new ArrayList(); // this code establishes a SQL connection, executes a // few queries and .Adds the results to the ArrayList return (a); }
In this code, GetServerInfo executes the "GetServerArray" routine asynchronously. This routine basically connects to a SQL Server, executes a couple of SELECT statements and returns the results in an ArrayList. After completion, the EndGetServerInfo is called, and I retrieve the results via ..EndInvoke.
Everything works great, and I get my ArrayList of information, but there's one small problem: in the EndGetServerInfo callback routine, I need to retrieve not just the returned ArrayList, but also the initial parameter I passed into the "GetServerInfo" routine (i.e., the "string server" variable).
I imagine there's an easy way to do this via the IAsyncResult, but I'm not sure how. Any ideas? Or would it be easier just to have my "GetServerArray" routine .Add the value of the "server" variable as the first item on the returned ArrayList? Seems kind of hokey, and would like to avoid that if possible.
Thanks, Michael C., MCDBA
|
|
|
| |
|
|
| |
| |
| Richard Blewett [DevelopMentor] (VIP) |
| GOOD ANSWER |
There is no automatic way to do this AFAIK but there is a mechanism available.
ad.BeginInvoke(server, new AsyncCallback(EndGetServerInfo), ad);
This last parameter is the AsyncState - you can pass anything in here so pack up your parameters in an object and pass it this way. Now the issue is "how do I get the delegate instance to call EndInvoke?"
Well it is documented (and therefore you can rely on it) that the IAsyncResult you get in a AsyncCallback is actually an AsyncResult instance. This class is in the System.Runtime.Remoting.Messaging namespace (where else ;-) ). So you can write your callback as follows:
Void Callback( IAsyncResult iar ) { AsyncResult ar = (AsyncResult)iar;
AsyncDelegate mydel = (AsyncDelegate)ar.AsyncDelegate; // AsyncDelegate is a property on the AsyncResult
MyParamsObject mp = (MyParamObject)ar.AsyncState;
... }
HTH
Regards
Richard Blewett - DevelopMentor http://staff.develop.com/richardb/weblog ? nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/ Hi all,
Is there an easy way to get the parameters of an asynchronous delegate call from the callback function? Here's an example of what I'm trying to do:
private delegate ArrayList AsyncDelegate (string server);
private void GetServerInfo (string server) { AsyncDelegate ad = new AsyncDelegate(GetServerArray); IAsyncResult iar = ad.BeginInvoke(server, new AsyncCallback(EndGetServerInfo), ad); }
private void EndGetServerInfo(IAsyncResult iar) { AsyncDelegate ad = (AsyncDelegate)iar.AsyncState; ArrayList a = ad.EndInvoke(iar); }
private ArrayList GetServerArray(string server) { ArrayList a = new ArrayList(); // this code establishes a SQL connection, executes a // few queries and .Adds the results to the ArrayList return (a); }
In this code, GetServerInfo executes the "GetServerArray" routine asynchronously. This routine basically connects to a SQL Server, executes a couple of SELECT statements and returns the results in an ArrayList. After completion, the EndGetServerInfo is called, and I retrieve the results via .EndInvoke.
Everything works great, and I get my ArrayList of information, but there's one small problem: in the EndGetServerInfo callback routine, I need to retrieve not just the returned ArrayList, but also the initial parameter I passed into the "GetServerInfo" routine (i.e., the "string server" variable).
I imagine there's an easy way to do this via the IAsyncResult, but I'm not sure how. Any ideas? Or would it be easier just to have my "GetServerArray" routine .Add the value of the "server" variable as the first item on the returned ArrayList? Seems kind of hokey, and would like to avoid that if possible.
Thanks, Michael C., MCDBA
--- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004
[microsoft.public.dotnet.languages.csharp]
|
|
|
| |
|
|
| |
| |
| Michael C |
| GOOD ANSWER |
The only problem is that currently my AsyncState is "ad", which returns my ArrayList results from the procedure I'm calling. If I replace the "ad" with the server name, I lose my actual results. I thought maybe there was a simple solution, like a property I was missing or something... Maybe the correct solution is to return an array of Objects, like this:
o[0] = (string) "servername" o[1] = (ArrayList) results
That, or just Add the server name onto the ArrayList as the first item as soon as I enter the GetServerArray function.
Thanks, Michael C., MCDBA
"Richard Blewett [DevelopMentor]" <Click here to reveal e-mail address> wrote in message news:%23gWW%Click here to reveal e-mail address... > There is no automatic way to do this AFAIK but there is a mechanism available. [Original message clipped]
"how do I get the delegate instance to call EndInvoke?" [Original message clipped]
This class is in the System.Runtime.Remoting.Messaging namespace (where else ;-) ). So you can write your callback as follows: [Original message clipped]
AsyncCallback(EndGetServerInfo), ad); } [Original message clipped]
queries and .Adds the results to the ArrayList return (a); } [Original message clipped]
couple of SELECT statements and returns the results in an ArrayList. After completion, the EndGetServerInfo is called, and I retrieve the results via ..EndInvoke. [Original message clipped]
retrieve not just the returned ArrayList, but also the initial parameter I passed into the "GetServerInfo" routine (i.e., the "string server" [Original message clipped]
routine .Add the value of the "server" variable as the first item on the returned ArrayList? Seems kind of hokey, and would like to avoid that if possible. [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Richard Blewett [DevelopMentor] (VIP) |
| GOOD ANSWER |
I already explained how to get hold of your async delegate instance without passing it via the AsyncState. Look for the section of my reply about the System.Runtime.Remoting.Messaging.AsyncResult class
Regards
Richard Blewett - DevelopMentor http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/ The only problem is that currently my AsyncState is "ad", which returns my ArrayList results from the procedure I'm calling. If I replace the "ad" with the server name, I lose my actual results. I thought maybe there was a simple solution, like a property I was missing or something... Maybe the correct solution is to return an array of Objects, like this:
o[0] = (string) "servername" o[1] = (ArrayList) results
That, or just Add the server name onto the ArrayList as the first item as soon as I enter the GetServerArray function.
Thanks, Michael C., MCDBA
"Richard Blewett [DevelopMentor]" wrote in message news:%23gWW%Click here to reveal e-mail address... > There is no automatic way to do this AFAIK but there is a mechanism available. [Original message clipped]
This class is in the System.Runtime.Remoting.Messaging namespace (where else ;-) ). So you can write your callback as follows: [Original message clipped]
|
|
|
| |
|
|
| |
| |
| Michael C |
| GOOD ANSWER |
Yeah, thanks for that. So I went ahead and inserted the server name at the top of the ArrayList that's returned.
Thanks, Michael C., MCDBA
"Richard Blewett [DevelopMentor]" <Click here to reveal e-mail address> wrote in message news:Click here to reveal e-mail address... > I already explained how to get hold of your async delegate instance without passing it via the AsyncState. Look for the section of my reply about the System.Runtime.Remoting.Messaging.AsyncResult class [Original message clipped]
correct solution is to return an array of Objects, like this: [Original message clipped]
|
|
|
| |
|
|
| |
|
|
|
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|