473,406 Members | 2,369 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,406 software developers and data experts.

Determine underlying Object in Asynchronous EndInvoke().

Hi

I call some "worker"-Methods asyncronous. here some simplified code

....

public delegate long AsyncWCaller(string parameter);

....

// Setting the Status-Delegate, worker object is given by parameter
Logger log = new Logger();
worker.StatusEventHandler += new Worker.StatusEvent( log.LogStatus);

// later on - in workerFinishd i will do
// worker.StatusEventHandler -= new
Worker.StatusEvent( log.LogStatus);
// Starting the Job async.
AsyncWCaller asyncW = new
AsyncWCaller(worker.DoSomethingWithNotification);

IAsyncResult wAsyncResult =
asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
asyncW);

-- ok
----

private void WFinished( IAsyncResult ar )
{

AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvoke(ar);

// !!!! My Problem is here
// 1. I will do some logging --result
// 2.I will remove the status event
// worker.StatusEventHandler -= new
Worker.StatusEvent( log.LogStatus);
//
// so how can i derermine the worker-Object here
}

Thanks Peter
Jul 28 '08 #1
3 1598
On Sun, 27 Jul 2008 23:53:25 -0700, Peter <pR****@procom-gmbh.comwrote:
[...]
private void WFinished( IAsyncResult ar )
{

AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvoke(ar);

// !!!! My Problem is here
// 1. I will do some logging --result
// 2.I will remove the status event
// worker.StatusEventHandler -= new
Worker.StatusEvent( log.LogStatus);
//
// so how can i derermine the worker-Object here
Typically, you would pass that as the last parameter of the
Delegate.BeginInvoke() method, and then retrieve the value from the
IAsyncResult.AsyncState property.

You are currently passing instead the delegate instance for that
parameter. I don't think you really need that, but if you do, then you
would have to create a separate data structure to hold both the delegate
instance reference as well as your Worker instance reference.

Depending on where the "worker" instance actually comes from, it's
possible there are even simpler ways to get at that reference. The code
sample you've posted is too vague to know. But the above should do it for
sure.

Pete
Jul 28 '08 #2
"Peter" <pR****@procom-gmbh.comwrote in message
news:ac**********************************@m73g2000 hsh.googlegroups.com...
[...]
AsyncWCaller asyncW = new
AsyncWCaller(worker.DoSomethingWithNotification);

IAsyncResult wAsyncResult =
asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
asyncW);
[...]
private void WFinished( IAsyncResult ar )
{
AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvoke(ar);
[...]
// so how can i derermine the worker-Object here
Basically, your problem is that you need to pass TWO things to your
callback routine: your "caller" delegate so that you can do
caller.EndInvoke, and also your "worker" object.
Since the AsyncState parameter is an object, you can pass there anything
that you want. One thing you can do is create a class to encapsulate both
things that you want to pass. Or you can use an object array, which is
itself an object:

IAsyncResult wAsyncResult =
asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
new object[]{asyncW, worker});
....
private void WFinished( IAsyncResult ar )
{
object[] state = (object[]) ar.AsyncState;
AsyncWCaller caller = (AsyncWCaller)state[0];
MyWorkerClass worker = (MyWorkerClass)state[1];
long result = caller.EndInvoke(ar);
...//use worker here
Jul 28 '08 #3
On 28 Jul., 09:59, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"Peter" <pRu...@procom-gmbh.comwrote in message

news:ac**********************************@m73g2000 hsh.googlegroups.com...
[...]
AsyncWCaller asyncW = new
AsyncWCaller(worker.DoSomethingWithNotification);
IAsyncResult wAsyncResult =
* *asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
asyncW);
[...]
private void WFinished( IAsyncResult ar )
{
* AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
* long result = caller.EndInvoke(ar);
[...]
*// * so how can i derermine the worker-Object here

* * Basically, your problem is that you need to pass TWO things to your
callback routine: your "caller" delegate so that you can do
caller.EndInvoke, and also your "worker" object.
* * Since the AsyncState parameter is an object, you can pass there anything
that you want. One thing you can do is create a class to encapsulate both
things that you want to pass. Or you can use an object array, which is
itself an object:

IAsyncResult wAsyncResult =
* * asyncW.BeginInvoke( "test", new AsyncCallback(WFinished),
* * new object[]{asyncW, worker});
...
private void WFinished( IAsyncResult ar )
{
* *object[] state = (object[]) ar.AsyncState;
* *AsyncWCaller caller = (AsyncWCaller)state[0];
* *MyWorkerClass worker = (MyWorkerClass)state[1];
* *long result = caller.EndInvoke(ar);
* *...//use worker here

Thank you....

not to see the wood for the trees (manchmal sieht man den Wald vor
lauter Bäumen nicht)
Peter
Jul 28 '08 #4

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

Similar topics

1
by: scott ocamb | last post by:
hello i am getting this error EndInvoke can only be called once for each asynchronous operation I have coded an app using the async method development pattern. This error occurred under...
9
by: Sam Loveridge | last post by:
Hi all. I'm relatively new to delegates and asynchronous threading and am running into an issue. I need to asynchronously call a method (which I'm doing with a delegate and BeginInvoke) and from...
3
by: Keyee Hsu | last post by:
Hi, I have a C# app that creates an AppDomain, enters it, and spawns an asyn thread to do some work and then block itself. Upon the completion of the work, the async thread supposedly terminates,...
4
by: bernardpace | last post by:
Hi, I am trying to get more familiar with asynchronous programming. I was reading through the document found on the page: ...
1
by: Mike | last post by:
I want a class to fire a method in another class, and once it fires it, to not care about what happens (i.e., if there are errors, return values, etc). I want this to happen asynchronously. I...
4
by: Pug Fugly | last post by:
I am unable to call the .Show() method on a form that I have passed through .BeginInvoke as the AsyncState parameter. I can get back the form correctly after the .EndInvoke is called in the...
4
by: 6954 | last post by:
Hi! i need to implement some asynchronous call to my com+ component, but i need it to return some values (e.g. results of sql select statement). obviously queued components and MSMQ are out of...
5
by: tcomer | last post by:
Hello, I'm working on an application that grabs some data from the web via HttpWebRequest. I'm using a local objects method to get the data, but the problem is that my form doesn't load until...
0
by: sirmoreno | last post by:
Hi, In my web site I have some long tasks that I want to call without delaying the page rendering - without making the thread that handels the page request wait for the long task to end. 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.