TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N_
Consider the fact that you use a bool as a signal, and later on in
the project you notice that you need to wait. Since events allow this
by definition, you would not need to change anything when you use
events.
Sure - but you've got more complicated code.
Events allow every operation that has to do with signaling. Bools
don't. That is at least one thing that is on the path of least
resistance, meaning that I don't have to change code (or mindset) if
I notice I need waiting.
But then, if you *don't* need waiting, you've ended up writing more
code (and harder to read code) for no reason.
Another issue, indeed less to the point, is that volatile is an ugly
beast in C/C++ and that it is strongly advised everywhere not to use
it for multi-threading purposes in C/C++. C# specifies the behaviour
of volatile more precisely, and for this program it will work, but
that doesn't make me use it at all.
I don't see why you're bringing C/C++ up again. We're talking about C#
- what is good or not good in different languages is entirely
irrelevant. Brining hang-ups or assumptions from one language to
another is a bad idea.
Volatile is something I would
only use when there is absolutely no other choice (e.g. when having
performance issues). Again, this is a matter of style. I think of
my way as the better way, because I am using an object that models an
event. Bool doesn't say anything. XXXEvent sais it all; we are dealing
with an event which is used to signal something.
To me, a bool which says "stopRunning" is a flag which is perfectly
readable - more so than an event, to be honest. Look at it in terms of
how the code reads - why should I be *waiting* on anything when all I
want to know is whether or not to stop running? Waiting on an event is
not a natural way of considering a simple condition - testing a boolean
is.
I don't mean to say that you should use an event where possible, I'm saying
not to use volatile bool unless you have no other choice (and I'm
guessing 99.99% of the times you do have other choices). Again, this
is my personal view on things. I am not an expert in multi-threading
apps, I just happen to be coding multi-threaded programs nowadays.
But your recommendation not to use volatile booleans is without merit,
and seems to be based on superstition coming from a C/C++ background.
There's no reason to particularly avoid volatiles, although as I've
said before (I think) I personally prefer using monitor locking, as it
then extends naturally to accessing to more than one variable at a
time.
I understand why you would choose not to use events. Why the
overhead? Then I think of something different: a programmer sees an
event and knows he is dealing with an event (by looking at its type
when defined). When the programmer sees a bool, he doesn't know it is
an event, unless he reads the documentation and/or the use of that
bool throughout the program.
The bool should, of course, be a well-documented property.
Alternatively, you could only set it from a public method, "Stop" or
suchlike - and then the actual implementation is *entirely* hidden from
the interface. If you wanted to use an event later on, you could do so
without changing the interface. Why expose an event at all if what
you're really wanting is a way of people saying, "I want you to stop
soon"?