queuebob
queuebob

Reputation: 121

how do I implement something like JavaScript's setTimeout in C#?

I have a C# app that receives updates from a server, processes it, and updates the GUI. the updates come in constantly, say several times a second. I want the app to update the GUI at most once every 2 seconds. so if an update comes in at time T, I want all updates that come in from T through T+2sec to stay in a buffer, and at T+2sec do the processing and GUI update. I know in JS you can use setTimeout() to execute some code at some time in the future, so I want something like that.

what's an appropriate way to do this? I've heard that using threads to "schedule" a function call isn't a great idea, but I'm not sure of a better way to do this. would it be so bad to use a Timer with a two second interval, synchronized to the GUI thread, that does the processing/updating?

Upvotes: 5

Views: 2894

Answers (2)

Guffa
Guffa

Reputation: 700730

You can use a System.Windows.Forms.Timer. It doesn't run events in a separate thread, it's the GUI thread that runs them. That way you can update the GUI directly without having to use Invoke.

Upvotes: 2

Oded
Oded

Reputation: 499302

You should use one of the timer classes, as you posted in your question.

See this MSDN article comparing them - this should give you a good basis to make a decision.

Upvotes: 0

Related Questions