views:

128

answers:

2

I have a case where I am getting lots of sockets requests coming in. I would like to update the UI 3 seconds after the last socket request has come in. E.g If socket request comes in and the previous one was only 2 seconds ago it should clear the UI Update and start waiting for 3 seconds.

Notes: Each Socket Request comes in on a different thread.

A: 

Use a System.Timers.Timer. Each time a request comes in you reset the timer for 3 seconds. When the timer goes off update the UI. The timer has to be accessed by every thread so you will need to protect it with a mutex.

Brian Ensink
+1  A: 

You can update the UI in a System.Timers.Timer (set the timer's SynchronizingObject property to your form) with a three-second interval and reset it whenever you receive a request.

EDIT: On further thought, you should use a System.Threading.Timer, which is less convenient then Timers.Timer but is more lightweight, and is completely thread-safe. In its callback, you'll have to call form.BeginInvoke to switch to the UI thread.

Each time you receive a request, you would run timer.Change(3000, -1).

SLaks