Как сделать задержку в c
sleep() function in C allows the users to wait for a current thread for a specific time. Other operations of the CPU will function properly but the sleep() function will sleep the present executable for the specified time by the thread.
Header Files Used
For the Windows platform, we can include windows.h library.
For the Linux platform, we can use unistd.h standard library. We can include this library as shown below
Return Value
The sleep() function in C returns 0 if the requested time has elapsed. Due to signal transmission sleep() returns the unslept quantity, a difference between the requested time to sleep() and the time it actually slept in seconds.
Как в C# добавить задержку при выполнении цикла?
Вообще если в Unity вовремя цикла сделать задержку надо, есть хороший вариант с Coroutine:
Дизайн сайта / логотип © 2023 Stack Exchange Inc; пользовательские материалы лицензированы в соответствии с CC BY-SA . rev 2023.3.11.43304
Нажимая «Принять все файлы cookie» вы соглашаетесь, что Stack Exchange может хранить файлы cookie на вашем устройстве и раскрывать информацию в соответствии с нашей Политикой в отношении файлов cookie.
C# Delay – How to pause code execution in C#
When programming, it is common to want to pause execution for a certain amount of time and most common programming and scripting languages have some form of Sleep command built in to achieve this.
For example, when we’ve encountered a problem with a remote resource, it’s common to back-off (pause execution) for a short amount of time and retry.
Here I’ll go through the various options for introducing a delay in C#, ranging from the most basic (Thread.Sleep()) – suitable for use in a single threaded console application. To more complicated versions for use in multi threaded interactive applications.
Add a delay in C# using Thread.Sleep()
Using Thread.Sleep() is the simplest way to introduce a delay in C# code, but it will hang the main thread for the duration of the delay, so it’s only really appropriate for console applications. Assuming you’re happy with that, let’s dive into a more complete example:
A common mistake when first using Thread.Sleep() is to forget the using, result in the following error:
This is easily fixed by adding a “using System.Threading;” line at the top of the file, as in the above example.
The next thing to note is that Thread.Sleep() takes miliseconds as it’s argument, so if you want to sleep for 3 seconds you need to pass the number 3000. It’s possible to make your intent clearer using a timespan like this:
But older versions of Thread.Sleep didn’t take a TimeSpan, so your mileage may vary.
Add a Delay in C# without blocking main thread using Task.Delay()
There is an asynchronous version of Thread.Sleep called Task.Delay. If you’re not familiar with how asynchronous calls work in C# then I’m planning a series of posts on the topic (let me know you want it in the comments!). Until that’s up, see the official docs if you need more info.
The idea here is to start a new task that runs the delay in the background, let’s look at an example:
Once again if you forget the using, you might encounter the following error:
Be sure to add “using System.Threading.Tasks;” to avoid this.
Note also that we’ve had to make our main method async and return a Task rather than void. This is a contrived example, and if you’ve got a console application as simple of this one there’s no need for this asynchronous version (just use sleep). Forgetting to do that can result in this error:
So what can we do with this asynchronous delay that can’t be done with the basic Thread.Sleep()? Quite simply, we can get other things done while we wait for the delay to finish. Time for a further example:
This example makes use of the fact that Task.Delay() is running in the background, and allows the main thread to do some useful (or not!) work. In this example the main thread just outputs “Waiting…
I hope I’ve not confused things by combining both Task.Delay() and Thread.Sleep() in one example!
For interactive (non-console) applications it’s especially important that the main thread can respond to inputs, allowing the program to remain interactive while delays are processed in the background.
Add a repeating delay in C# using one of the Timer classes
There are several timer classes that can be used to repeatedly trigger events after a certain period of time has elapsed:
Each of these timer classes has different functionality, with these remarks on MSDN giving more details of which to use depending on your requirements.
If you just want a single delay in C# then use either Thread.Sleep() or Task.Delay() as described above. However, if you’re after a repeating delay, a timer can be helpful.
For the purposes of the following examples, I’m going to use a System.Threading.Timer() as it appears to be Microsoft preferred general purpose timer.
- callback TimerCallback – this is the method that should be called whenever the timer fires
- dueTime int/Timespan – this is how long to wait before the timer first fires
- period int/TimeSpan – this is how long to wait between each subsequent firing of the timer
As example of such an instantiation might be:
This starts a timer that will wait 5 seconds before calling DoSomething, which it will continue to do once a second after that.
The following example is more complete, showing you how to set up the callback, one way of tracking the number of times it’s called, and how to signal that the timer should finish and then stop it. Here’re the code:
I know it’s a bit heavy, but in the above example I’ve created a new class IdleWaiter which is responsible for printing “Waiting…” each time it’s called, while tracking the number of times it’s been called and signalling (via an autoResetEvent) when it’s reached a threshold.
When you run this code, the timer fires every seconds until it’s been run three times, then it signals that it’s reached it’s threshold and we stop the timer by disposing of it.
If we didn’t dispose of the timer it would keep on ticking once every second. You can try this for yourself by commenting out the dispose line and adding a Thread.Sleep() to stop the program exiting:
If you run the above code with this change you get the following output:
Using a Timer might be the right choice if you want a task to repeat on a schedule, but given the added complexity, I’d probably stick to the other options for most use cases.
Conclusion
If you’re in a console app, or some other single threaded application, you can use Thread.Sleep() to trigger a delay, just be careful with the fact this takes milliseconds (or better yet use TimeSpan.FromSeconds()).
If you want to delay in an asynchronous way, allowing your main thread to do useful work in the interim, Thread.Delay() is they way to go.
If you’re want to kick something off repeatedly with a delay in between, then you should be using a timer like System.threading.Timer.
I’m sure there are other ways of adding a delay in C#, but I think I’ve covered off the most important ones. If there’s something you think I’ve missed that deserved to be included, if you think I’ve got something wrong, or if you just want to congratulate me on a job well done then please let me know in the comments.
Mean while, if you want to go deep on another area of C#, might I recommend my recent post on using (or abusing?) Linq style foreach.
report this ad
Как организовать задержку выполнения программы в C
wikiHow работает по принципу вики, а это значит, что многие наши статьи написаны несколькими авторами. При создании этой статьи над ее редактированием и улучшением работали, в том числе анонимно, 11 человек(а).
Количество просмотров этой статьи: 27 713.
Вы когда-нибудь хотели создать программу на C, которая выжидает определенное время? Вы можете настроить способ, чтобы дать времени «пролететь», например: при показе всплывающей страницы (уведомление или подсказка) для игры. . ОК, вот несколько способов создания программы вида «stand still» (стоять на месте), читайте дальше .