BeginInvoke on events with callback problem

G

Guest

Hi guys 'n' girls,
I want to use callback methods when using BeginInvoke on some events. So far
no problem, but know I thought about what could happen (if I'm not completly
wrong).

Lets say an event is triggerd with BeginInvoke and a callback funktion which
is part of the instanze which is also holding the event. So the call looks
something like this:

public class MyClass {
public void someMethod(object myParameter) {
this.MyEvent.BeginInvoke(myParameter, new
AsyncCallback(this.MyCallBack), this.MyEvent);
}

private void MyCallBack(IAsyncResult ar) {
MyDelegate md = (MyDelegate)ar.AsyncState;
md.EndInvoke(ar);
}
}

So know I've two problems (at least I think they are problems):

1. What happens if lets say something calls someMethod(..) and during the
execution of the event (which takes some time) something other calls the
method again. How does the calling of EndInvoke know which one to "finish"? I
think I just don't understand the whole thing, so I hope someone can help me
with that?

2. As my application is a service it can get stopped. The objects which are
using the above code should be destroyed at shutdown, using Dispose(). But
what if someMethod(...) has been called and not jet finished while Dispose()
gets called? In the above code it's no big deal, but even here couldn't it
happen, that the garbage collector gets the object before the callback method
can do its work? As I see it, in a worst case szenario, the BeginInvoke
starts some processing and meanwhile the object with the callback method gets
destroyed. Can this happen? And if yes, how can I solve this problem?

geetings

Florian
 
G

Guest

1. What happens if lets say something calls someMethod(..) and during the
execution of the event (which takes some time) something other calls the
method again.

Anything that implements the ISynchronizeInvoke interface properly will
address this for you. For example GUI objects like Forms implement
ISynchronizeInvoke such that a windows message is sent to the thread that
created the GUI objectl {a requirement for GUI objects} - the event handler
is called by the thread that created the control. Subsequent events will
'stack up' in the windows message queue until the GUI thread gets a chance to
process them; aka there will be no re-entrancy issues...
How does the calling of EndInvoke know which one to "finish"?

BeginInvoke returns an instance of IAsyncResult. The IAsyncResult instance
"knows" which particular call to the event it is waiting on...
2. As my application is a service it can get stopped.
... <snip> ...
As I see it, in a worst case szenario, the BeginInvoke
starts some processing and meanwhile the object with the callback method gets
destroyed. Can this happen? And if yes, how can I solve this problem?

Yes you can be stopped. If a stop command is issued the OS will destroy the
process if it fails to stop after 30 seconds or so. There was a message
thread about a month ago in this newsgroup that discussed ways to extend the
OS stop timeout - search for service/stop/SetServiceStatus()....

Due to the way that .NET garbage collection works your object will not be
garbage collected until all references to it are released. However some
other thread COULD call Dispose() thus closing a handle or releasing a COM
object out from under you...

This is an architectural issue; you need to design your service for
'stop-ability'. When a user stops your service the OS will signal you
asynchronously via the stop handler {on an OS thread}. Typically the stop
handler should do something quick like set an event or flag variable - then
your service's worker threads should tear down your instantiated objects in
some organized fashion...
 
G

Guest

First of all, thank you very much for your answer, but I haven't understood
the following completly:
BeginInvoke returns an instance of IAsyncResult. The IAsyncResult instance
"knows" which particular call to the event it is waiting on...

Do I have to use the returned IAsyncResult in any way or does the system all
the work for me?

greetings

Florian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top