Другие языки программирования и технологии

Процедура Delphi, приостанавливаещая работу программы на заданное время.

Mig Mig
Mig Mig
17 297
Используйте Sleep (Milliseconds). Программа перестает работать на указанное в скобках количество тысячных секунд. Погрешность до 10 мсек. При этом способе ничего не происходит, даже перерисовка.

Чтобы этого избежать можно использовать другой способ:

procedure TForm1.Button1Click(Sender: TObject);
var
t: integer;
begin
t := GetTickCount;
repeat
Application.ProcessMessages
until
GetTickCount - t >= 1000;
Button1.Caption := Button1.Caption + '1';
end;
НС
Никита Смусев
73 776
Лучший ответ
procedure TForm1.Button1Click(Sender: TObject);
begin
while true do
begin
// бесконечный цикл
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage('Нажата кнопка Button1');
end;
Sleep

The Sleep function suspends the execution of the current thread for at least the specified interval.

To enter an alertable wait state, use the SleepEx function.

VOID Sleep(
DWORD dwMilliseconds
);

Parameters
dwMilliseconds
[in] Minimum time interval for which execution is to be suspended, in milliseconds.
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution.

A value of INFINITE indicates that the suspension should not time out.

Return Values
This function does not return a value.

Remarks
This function causes a thread to relinquish the remainder of its time slice and become unrunnable for at least the specified number of milliseconds, after which the thread is ready to run. In particular, if you specify zero milliseconds, the thread will relinquish the remainder of its time slice but remain ready. Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the specified interval elapses. For more information, see Scheduling Priorities.

You have to be careful when using Sleep and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. If you have a thread that uses Sleep with infinite delay, the system will deadlock. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than Sleep.