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

Delegates and Events

When multiple objects (say ClockA, ClockB, ClockC ...) from the same class
(Clocks)have the same delegate ( say secondTick) that calls the event handler
serviceClockTick, do the events stack up in a some sort of a queue, do they
preempt each other safely, does a preempting event push an existing event out
of the queue, or do they run amok? This example assumes that all the clocks
are closely synchronised and call the event handler pretty much
simultaneously. Please comment.

I'm finding that with this example with six clocks the event handler misses
at least one event, sometimes two, on a regular basis. Any solutions?
Nov 17 '05 #1
4 2571
Hi,

Everytime you delegate an event using += is adding the callback reference to
a queue, this queue is executed as FIFE (First in First executed), because
you have several clocks running the same event and maybe at the same time you
should include thread safe to the function. I suspect that both calls are
trying to use the same function and clashes because MutEx (Mutual Exclusion).
Try to lock the function and see if the problem is still on.

Hope this helps
Salva

"FredC" wrote:
When multiple objects (say ClockA, ClockB, ClockC ...) from the same class
(Clocks)have the same delegate ( say secondTick) that calls the event handler
serviceClockTick, do the events stack up in a some sort of a queue, do they
preempt each other safely, does a preempting event push an existing event out
of the queue, or do they run amok? This example assumes that all the clocks
are closely synchronised and call the event handler pretty much
simultaneously. Please comment.

I'm finding that with this example with six clocks the event handler misses
at least one event, sometimes two, on a regular basis. Any solutions?

Nov 17 '05 #2
Hi,

Everytime you delegate an event using += is adding the callback reference to
a queue, this queue is executed as FIFE (First in First executed), because
you have several clocks running the same event and maybe at the same time you
should include thread safe to the function. I suspect that both calls are
trying to use the same function and clashes because MutEx (Mutual Exclusion).
Try to lock the function and see if the problem is still on.

Hope this helps
Salva

"FredC" wrote:
When multiple objects (say ClockA, ClockB, ClockC ...) from the same class
(Clocks)have the same delegate ( say secondTick) that calls the event handler
serviceClockTick, do the events stack up in a some sort of a queue, do they
preempt each other safely, does a preempting event push an existing event out
of the queue, or do they run amok? This example assumes that all the clocks
are closely synchronised and call the event handler pretty much
simultaneously. Please comment.

I'm finding that with this example with six clocks the event handler misses
at least one event, sometimes two, on a regular basis. Any solutions?

Nov 17 '05 #3
This explains a lot. Thank you. How do I add thread safety and lock the
function?

"Salvador" wrote:
Hi,

Everytime you delegate an event using += is adding the callback reference to
a queue, this queue is executed as FIFE (First in First executed), because
you have several clocks running the same event and maybe at the same time you
should include thread safe to the function. I suspect that both calls are
trying to use the same function and clashes because MutEx (Mutual Exclusion).
Try to lock the function and see if the problem is still on.

Hope this helps
Salva

"FredC" wrote:
When multiple objects (say ClockA, ClockB, ClockC ...) from the same class
(Clocks)have the same delegate ( say secondTick) that calls the event handler
serviceClockTick, do the events stack up in a some sort of a queue, do they
preempt each other safely, does a preempting event push an existing event out
of the queue, or do they run amok? This example assumes that all the clocks
are closely synchronised and call the event handler pretty much
simultaneously. Please comment.

I'm finding that with this example with six clocks the event handler misses
at least one event, sometimes two, on a regular basis. Any solutions?

Nov 17 '05 #4
Hi

In order to lock a function you need to lock the object, if you want to lock
all the function you can use "this" object.

private static void MyFnc()
{
lock (this)
{
// Executes
}
}

This lock is for reading and writing, so if two threads try to execute the
function one will wait for the other to finish. If you need more control you
can have a ReadLock or a WriteLock so you can have more control over the
locking environment. Finally you can lock a single object like:

lock (MyCollection)
{

}

Hope this helps,
Regards
"FredC" wrote:
This explains a lot. Thank you. How do I add thread safety and lock the
function?

"Salvador" wrote:
Hi,

Everytime you delegate an event using += is adding the callback reference to
a queue, this queue is executed as FIFE (First in First executed), because
you have several clocks running the same event and maybe at the same time you
should include thread safe to the function. I suspect that both calls are
trying to use the same function and clashes because MutEx (Mutual Exclusion).
Try to lock the function and see if the problem is still on.

Hope this helps
Salva

"FredC" wrote:
When multiple objects (say ClockA, ClockB, ClockC ...) from the same class
(Clocks)have the same delegate ( say secondTick) that calls the event handler
serviceClockTick, do the events stack up in a some sort of a queue, do they
preempt each other safely, does a preempting event push an existing event out
of the queue, or do they run amok? This example assumes that all the clocks
are closely synchronised and call the event handler pretty much
simultaneously. Please comment.

I'm finding that with this example with six clocks the event handler misses
at least one event, sometimes two, on a regular basis. Any solutions?

Nov 17 '05 #5

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

Similar topics

4
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that...
0
by: Steven Brown | last post by:
I'm trying to figure out how to safely use .NET events/delegates in a thread-safe class. There are a couple problems. One is that the standard "if(EventName != null) EventName(...);" call can...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
3
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.