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

BeginInvoke on events with callback problem

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
Nov 17 '05 #1
2 2515
> 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...


Nov 17 '05 #2
First of all, thank you very much for your answer, but I haven't understood
the following completly:
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...


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

greetings

Florian
Nov 17 '05 #3

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

Similar topics

3
by: HankC | last post by:
Greetings: I'm been programming Delphi for a while and am trying to get into the Python mindset. In Delphi, you can have a component class that has properties, methods, and events. Properties...
9
by: David Sworder | last post by:
Hi, I have a form that displays data (is that vague enough for you?). The data comes in on a thread-pool thread. Since the thread pool thread is not the same as the UI thread, the callback...
3
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
7
by: nick_tucker | last post by:
Is The following Code valid it is like a mini thread pool(a friend at work wrote it)? I have cut down the code a bit but kept the essential parts. The main part that I am would like explainedis...
2
by: Joe | last post by:
In the callback I need to call an event letting the calling form know that the operation completed. If the operation fails, the main form needs to show another form. The problem I get is when the...
5
by: GT | last post by:
Could someone please explain the difference of (refer to example code below) d.BeginInvoke("some text", null,null); //Alternative A and BeginInvoke( d, new object { "some text" } ...
2
by: Flack | last post by:
Hello, If I understand BeginInvoke correctly, when it is called your delegate is run on a thread pool thread. Now, if you supplied a callback delegate, that too is called on the same thread...
9
by: Bob Altman | last post by:
I'm trying to call a function on a background thread in a C++/CLI (VS 2005) application compiled with /clr. I store a delegate reference in a module-level gcroot. When I call BeginInvoke on the...
7
by: Ben Voigt [C++ MVP] | last post by:
As much as the CLR team assures us that it's ok to fire-and-forget Control.BeginInvoke, it seems it isn't. Maybe this is a bug. See for example: the comments in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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...

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.