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?
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.