serdar
serdar

Reputation: 1

Windows.Forms.Timers tick and button click at the same time in c#

I have a button and a Windows.Forms.Timers timer1. Both of them have some operations on database. Could couse any problem on Main thread if timer1 tick fired when button click is already operating ? What is the Main thread response in this case ? Both of them operates seperately or button click operation blocked by timer1 tick ?

Upvotes: 0

Views: 286

Answers (1)

Michael Spradlin
Michael Spradlin

Reputation: 21

If you aren't implementing any async operations, this cannot happen. When an event is raised on the UI thread, whether a Form Timer or a Button Press, the UI is locked for the duration (as the thread handling the rendering is busy doing something else). In the event where you click a button while a timer_tick is running, the UI won't begin processing the button_clicked event until the timer_tick event returns.

If you are implementing any await/async operations or running any background workers, then things get a little more complicated because the timer_tick event could be awaiting a result from a background thread and begin processing the button_clicked event before that has returned. However, unless you are implementing threading yourself this cannot happen.

Upvotes: 2

Related Questions