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
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