473,385 Members | 1,720 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,385 software developers and data experts.

Asynchronous Delegate w/ Callback Parameters

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
Nov 16 '05 #1
4 2219
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]

Nov 16 '05 #2
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]" <ri******@develop.com> wrote in message
news:%2******************@TK2MSFTNGP15.phx.gbl...
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]

Nov 16 '05 #3
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:%2******************@TK2MSFTNGP15.phx.gbl...
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


Nov 16 '05 #4
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]" <ri******@develop.com> wrote in message
news:eb*************@TK2MSFTNGP11.phx.gbl...
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:%2******************@TK2MSFTNGP15.phx.gbl...
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

Nov 16 '05 #5

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

Similar topics

1
by: Sai Kit Tong | last post by:
I have to interface managed application with my legacy dll. I have employed the wrapper approach but I have to deal with the asynchronous callback from the legacy dll, which likely goes through a...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
2
by: Gerda | last post by:
Hi! I've implemented many times an asynchronous call of a method with a call backfunction successfully. But to implement this with VB.NET is not so successfully. I can implement all events...
2
by: Ann Huxtable | last post by:
Hi, I want to do two types of async callbacks in C#. One involves spawning a worker thread, and the other does not invlve threads: Case 1 (Span new thread) ---------------------------- class...
1
by: MSDN | last post by:
Does anyone know how to do this with a readline statement or equivalent method? Thanks in advance. Chris
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
10
by: Susan | last post by:
I have a process that takes a while to run so I have it running asynchronously so that the user can continue working. My question is that I want to killl the process if the user changes the search...
3
by: =?Utf-8?B?bWs=?= | last post by:
Hi everyone, I need to refactor some of our processes using the asynchronous programming model. I have defined a couple of arrays of delegates to pipline the asynchronous invocations through...
1
by: Morgan Cheng | last post by:
Asynchronous method model generally has a callback delegate as argument. The delegate will be called when some WaitHandle is set or time out. And, one more argument is used as state object to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...
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...

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.