tags:

views:

71

answers:

2

In WPF app I have a timer which cyclically performs some database quering operations (LINQ to SQL) and visual controls updating operations by calling a particular method.

Sometimes I need to call this the same method by UI events (button clicks, for example).

Is there any danger, in case timer-based calling and UI event-based calling of this the same method take place simultaneiously? Or .NET framework protects me from a danger like this?

Is using one method in a way like this totally OK?

+1  A: 

Well, what does the method do, exactly? If it updates the UI, you'll need to make sure you marshall back to the UI thread (using a Dispatcher) for that part.

Does the method touch any shared state? If so, again you'll need to be careful.

Basically there's nothing inherently unsafe about a single method being called from two threads simultaneously... but equally there's nothing to automatically protect you from doing unsafe things (in terms of concurrency) within that method.

Jon Skeet
Thanks Jon! Please, help me to understand, If I use DispatcherTimer and its dispatcherTimer.Tick event handler, does it mean that everything done from this "dispatcherTimer.Tick event handler" is done on different (new) thread?
rem
It doesn't. You're fine, that timer can only Tick when the UI thread is idle. No locking is required.
Hans Passant
A: 

The timer and click event would be running on different threads and could execute the method at the same time. It may be worth putting a synclock around any critical section within the method. i.e. if the code calculates a value and etc.

Andrew
Locks shouldn't be introduced in a "just in case" fashion. Calculating a value is absolutely fine - if it doesn't touch shared state.
Jon Skeet