Всем, часто приходилось использовать в Delphi Функцию Sleep, в результате которой программа не отвечает N количество миллисекунд затормаживая процесc, предлагаю использовать альтернативу:
procedure pause(col:integer);
var wtim:ttime;
begin
wtim:=encodetime(0,0,col,0)+time;
repeat
application.processmessages;
sleep(10);
until time>=wtim;
end;
col — пауза в секукндах.
При использовании данной функции, программа не будет уходить в Даун, а будет дальше функционировать.
Пример 1:
В приведенном выше примере программа уйдёт в Даун на 5 секунд и будет не доступна, после вывода сообщения «Привет«
Как сделать паузу в дельфи?
Примитивный пример:
——-CODE———
Begin
ShowMessage(‘РАЗ’);
// Строка которая будет делать паузу
ShowMessage(‘ДВА’);
end;
—-END-CODE——
P.S. SLEEP() — НЕ ПРЕДЛАГАТЬ! НЕ ОТДЕЛЬНО, НЕ С «Application.ProcessMessages».
Нужен другой альтернативный вариант.
1 сек = 1000 милисек
пропустил комент про слип
Я в таких случая пользуюсь Timer-ом. Просто вешаю на форму таймер. Задаю в нем интервал времени и делаю так:
begin
ShowMessage(‘РАЗ’);
Timer1.Endblr:=True;
end;
I’ve got an algorithm. I’d like to pause it at some point and then continue once the user presses a button. How do I do that? I’ve browsed the documentation, and searched the internet, but no luck yet.
Here’s a relevant code snip:
Right now, I use sleep (pauza is just a constant, means pause in Serbian). Ideally, I’d like to replace that line with a procedure which would sleep for the interval, or wait for a button press based on a configuration setting.
EDIT1: Ah yes, if it wasn’t obvious — it’s a graphics application, not console, so slapping a «readln» won’t work (sadly).
7 Answers 7
I wouldn’t recommend it as good application design, but if none of the other suggestions are suitable for you, you may prefer this.
Add a ‘Paused: Boolean’ field to your class/form, and a ‘Continue’ button.
When you start the operation, set Paused to False, and Continue.Enabled := False;
When your code reaches the section where you want to pause:
In your Continue buttons event handler:
As I said before, not pretty, but it should get the job done.
That’s not how you do event-driven programming. You should do the processing up to the pause point and end your function at that point. In a button press event handler you then do the remainder of the processing — this will only happen when the user presses the button. You can call the same code from an OnTimer event to continue the processing after a given peruiod.
Use a thread and the built-in suspend/resume functions for the TThread class. Jens Borrisholt wrote a nice little example article on about.com here doing this kind of thing.
If you introduce multiple ProcessMessage locations in the application, you can have problems with code execution from other events being left running when the form closes. For instance, if the form was closed while in the ProcessMessage «sleep» mode, there is no good way to unwind the stack.
MessageDialog may not work if you are using DirectX/OpenGL since you won’t have the normal display open. Calling MessageDialog may switch the view from the DirectX view to normal desktop view (ugly but workable) OR it may show the message on the normal desktop view without changing the display mode from DirectX (making it impossible for the user to see the prompt). It’s been a long time since I’ve worked with Dx, but I recall issues about failing to change view modes being a pain.