Callbacks between threads

Pickle

Newcomer
Joined
Mar 1, 2006
Messages
18
I have a thread that I want to act as a clock. It read a hardware counter and keeps track of the counts that occur. This clock thread will also have a public method of receiving time durations from other threads to signal those threads when the duration is done.

My problem is the interface between the clock thread and the worker threads. My idea would be to have the worker thread give the clock thread an time duration and address to a function to callback. So when the time duration had elasped then the clock thread calls/invokes the function given to it from the worker thread.

Is there a way to pass this callback from worker thread to the clock thread? Or is there a completely better way to do what I want?
 
Is there a way to pass this callback from worker thread to the clock thread?
What about passing a delegate in through the constructor or through a SetAlert(time, delegate) method or something like that? The only trick is that you'll have to be careful about cross thread calls when invoking the delegate.

Or is there a completely better way to do what I want?
Another way might be to use events, which is actually very similar to using delegates. The idea is that you would fire an alarm event and then whatever threads are currently listening could determine if that event was meant for them. The event might have requester and time parameters so that the handler can verify that the event is meant for them. The handler, if consuming the event would need to cancel the event if it is used.

This method may not scale well as the worst case is the total number of worker threads. If you have less than a dozen (maybe?) concurrent worker threads then I don't think scalability would be an issue but that would depend on the machine/cpu.

Knowing nothing about your project, I would probably try the delegate idea first because it involves less code.
 
What about passing a delegate in through the constructor or through a SetAlert(time, delegate) method or something like that? The only trick is that you'll have to be careful about cross thread calls when invoking the delegate.

The delegate idea is what im thinking might work the best, I to have thought about firing an event, but I dont really want to have all of worker items firing off to check the event to see if its the correct one.

Right now im looking into AsyncCallback Delegate, does this sound like what your refering to?
 
It looks like the AsyncCallback delegate should work just fine. It appears to capture the basic idea that I had in mind. Plus it's free to boot.
 
Back
Top