Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# timer (slowing down a loop)

Tags:

c#

.net

I would like to slow down a loop so that it loops every 5 seconds.

In ActionScript, I would use a timer and a timer complete event to do this. How would I go about it in C#?

like image 273
Chin Avatar asked Aug 15 '10 08:08

Chin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C full form?

Full form of C is “COMPILE”.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

You can add this call inside your loop:

System.Threading.Thread.Sleep(5000); // 5,000 ms

or preferable for better readability:

System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));

However, if your application has a user interface you should never sleep on the foreground thread (the thread that processes the applications message loop).

like image 90
Martin Liversage Avatar answered Sep 22 '22 03:09

Martin Liversage