473,788 Members | 2,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine underlying Object in Asynchronous EndInvoke().

Hi

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

....

public delegate long AsyncWCaller(st ring parameter);

....

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

// later on - in workerFinishd i will do
// worker.StatusEv entHandler -= new
Worker.StatusEv ent( log.LogStatus);
// Starting the Job async.
AsyncWCaller asyncW = new
AsyncWCaller(wo rker.DoSomethin gWithNotificati on);

IAsyncResult wAsyncResult =
asyncW.BeginInv oke( "test", new AsyncCallback(W Finished),
asyncW);

-- ok
----

private void WFinished( IAsyncResult ar )
{

AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvok e(ar);

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

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

AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvok e(ar);

// !!!! My Problem is here
// 1. I will do some logging --result
// 2.I will remove the status event
// worker.StatusEv entHandler -= new
Worker.StatusEv ent( log.LogStatus);
//
// so how can i derermine the worker-Object here
Typically, you would pass that as the last parameter of the
Delegate.BeginI nvoke() method, and then retrieve the value from the
IAsyncResult.As yncState 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****@proco m-gmbh.comwrote in message
news:ac******** *************** ***********@m73 g2000hsh.google groups.com...
[...]
AsyncWCaller asyncW = new
AsyncWCaller(wo rker.DoSomethin gWithNotificati on);

IAsyncResult wAsyncResult =
asyncW.BeginInv oke( "test", new AsyncCallback(W Finished),
asyncW);
[...]
private void WFinished( IAsyncResult ar )
{
AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
long result = caller.EndInvok e(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.EndInvok e, 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.BeginInv oke( "test", new AsyncCallback(W Finished),
new object[]{asyncW, worker});
....
private void WFinished( IAsyncResult ar )
{
object[] state = (object[]) ar.AsyncState;
AsyncWCaller caller = (AsyncWCaller)s tate[0];
MyWorkerClass worker = (MyWorkerClass) state[1];
long result = caller.EndInvok e(ar);
...//use worker here
Jul 28 '08 #3
On 28 Jul., 09:59, "Alberto Poblacion" <earthling-
quitaestoparaco ntes...@poblaci on.orgwrote:
"Peter" <pRu...@proco m-gmbh.comwrote in message

news:ac******** *************** ***********@m73 g2000hsh.google groups.com...
[...]
AsyncWCaller asyncW = new
AsyncWCaller(wo rker.DoSomethin gWithNotificati on);
IAsyncResult wAsyncResult =
* *asyncW.BeginIn voke( "test", new AsyncCallback(W Finished),
asyncW);
[...]
private void WFinished( IAsyncResult ar )
{
* AsyncWCaller caller = (AsyncWCaller) ar.AsyncState;
* long result = caller.EndInvok e(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.EndInvok e, 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.BeginInv oke( "test", new AsyncCallback(W Finished),
* * new object[]{asyncW, worker});
...
private void WFinished( IAsyncResult ar )
{
* *object[] state = (object[]) ar.AsyncState;
* *AsyncWCaller caller = (AsyncWCaller)s tate[0];
* *MyWorkerClass worker = (MyWorkerClass) state[1];
* *long result = caller.EndInvok e(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
7069
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 stress testing
9
1790
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 the callback method, or at some point after the EndInvoke has been called to end the asynchronous operation I need to asynchronously call a different method. I really want to keep this event based and use delegates and callbacks rather than loop...
3
2889
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, then the original thread unblocks, unloads the AppDomain, and starts the whole process all over again. I get the System.AppDomainUnloadedException saying that the AppDomain from which the async thread resides has been unloaded. Now, if I were...
4
348
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: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp Now, I read that 'Always call EndInvoke after your asynchronous call completes'.
1
1559
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 read this post: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=812beb6be4fd2339&rnum=2 And the MS guy says that calling EndInvoke does more than simply wait for the end of the call. Well, what *does* it do? Must I really call
4
2300
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 callback, but the process still seems to be on a different thread even though the ..EndInvoke finished running. I get "Controls created on one thread cannot be parented to a control on a different thread." Which I know is not allowed, but I thought...
4
2210
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 the question... anyone has any ideas how to implement it? or just a guideline maybe? thank you
5
1702
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 this method has finished what it's doing.. which takes about 10-15 seconds. Here is what I have: Using System.Threading;
0
1385
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 found two solutions but I can't figure out which one is better? 1. Asynchronous OneWay WebService: - Declare a OneWay method for the long task inside an asmx file in my website and WebReference it.
0
9656
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
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
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...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.