"FredC" <Fr***@discussi ons.microsoft.c omwrote in message
news:EC******** *************** ***********@mic rosoft.com...
>I have an application that uses mulitple timers. Each of the timer event
handlers manipulate a common array of data.
Which Timer class are you using?
There's System.Windows. Forms.Timer, System.Threadin g.Timer,
System.Timers.T imer -- and they all act very different!
System.Windows. Forms.Timer will pass WM_TIMER, causing your handler to be
called on the main thread in between user actions. They will be serialized,
and locking does nothing if you're not pumping messages (think
Application.DoE vents) during a callback, and if you are -- deadlock with
non-recursive locks, and data corruption with recursive locks. But if you
prevent re-entrancy by not pumping messages during the timer processing,
everything is much simpler.
The others will create background worker threads. Locking is applicable
here.
I'm getting Null refererance errors - should I put a lock on the array
when
I change a value?
As well, should I lock the array when I only want to read a value?
If you're resizing the array, you must lock writes. Otherwise locks
shouldn't be necessary. Locking reads shouldn't be needed, although after a
resize your reads could be accessing an old copy for an extended length of
time.