Home > Back-end >  Delphi why execution time delay function can't close the window?
Delphi why execution time delay function can't close the window?

Time:09-24

Online to find a don't hang don't delay function of the CPU, in use process, found the loop can't close the window,
By rights added: Application. ProcessMessages; Not should be able to respond to other message?
Why can't the news of the response to close the window? Looked great god help solve, thank you very much!
Delphi2010

Function parts:
 
Procedure TForm2. Delay (msecs: integer);//super delay
Var
Tick: DWord;
Event: THandle;
The begin
Event:=CreateEvent (nil, False, False, nil);
Try
Tick:=GetTickCount + dwords (msecs);
While (msecs & gt; 1, 0) and (MsgWaitForMultipleObjects (Event, False, msecs, QS_ALLINPUT) & lt;> WAIT_TIMEOUT) do
The begin
Application. ProcessMessages;//handle events ()
Msecs:=Tick - GetTickcount;
end;
The finally
The CloseHandle (Event);
end;
end;



Application part (click on the button here, after the loop is not performed can't close the window) :
 
Procedure TForm2. Button3Click (Sender: TObject);
Var I: Integer;
The begin
I:=0;
For I:=0 to 10 do
The begin
ShowMessage (IntToStr (I));
Delay (2000);
end;
end;

CodePudding user response:

Don't recommend you this kind of writing, don't hang don't delay function of the CPU, please use the timer,

CodePudding user response:

reference 1st floor pathletboy response:
don't recommend you this kind of writing, don't hang don't delay function of the CPU, please use the timer,


Timer is not convenient, such as I want to write a similar script program
For example (write pseudo-code)
The mouse (x1, y1);
Time delay (1000);
The mouse to click ();
Time delay (500);
.
Such, use the timer is certainly inconvenient
Whether to use delay function, but if with that, the main building has launched a cycle, can't close the window, is certainly not ah, what's the solution?

CodePudding user response:

refer to the second floor bowen666 response:
Quote: refer to 1st floor pathletboy response:

Don't recommend you this kind of writing, don't hang don't delay function of the CPU, please use the timer,


Timer is not convenient, such as I want to write a similar script program
For example (write pseudo-code)
The mouse (x1, y1);
Time delay (1000);
The mouse to click ();
Time delay (500);
.
Such, use the timer is certainly inconvenient
Whether to use delay function, but if with that, the main building has launched a cycle, can't close the window, is certainly not ah, what's the solution?

This requirement, you should use a thread,

CodePudding user response:

Sleep (1000);

CodePudding user response:

reference pathletboy reply: 3/f
Quote: refer to the second floor bowen666 response:

Quote: refer to 1st floor pathletboy response:

Don't recommend you this kind of writing, don't hang don't delay function of the CPU, please use the timer,


Timer is not convenient, such as I want to write a similar script program
For example (write pseudo-code)
The mouse (x1, y1);
Time delay (1000);
The mouse to click ();
Time delay (500);
.
Such, use the timer is certainly inconvenient
Whether to use delay function, but if with that, the main building has launched a cycle, can't close the window, is certainly not ah, what's the solution?

This requirement, you should use threads,


Ok thank you I try thread

CodePudding user response:

reference 4 floor lyhoo163 response:
sleep (1000);


Sleep is suspends program card dead brother.

CodePudding user response:

While (msecs & gt; 1, 0) and (MsgWaitForMultipleObjects (Event, False, msecs, QS_ALLINPUT) & lt;> WAIT_TIMEOUT) do

Add a global status word teminateit: Boolean

While (not teminateit) and (msecs & gt; 1, 0) and (MsgWaitForMultipleObjects (Event, False, msecs, QS_ALLINPUT) & lt;> WAIT_TIMEOUT) do

In front of the window, put: teminateit:=true;

CodePudding user response:

refer to 6th floor bowen666 response:
Quote: refer to 4th floor lyhoo163 response:

Sleep (1000);


Sleep is suspends program the eldest brother stuck.


Sleep is waiting,

CodePudding user response:

Don't thread can be achieved, see below,

Procedure Delay (i_Msc: integer);
Var
Msg: TMsg;
The begin
//start a timer, timer interval, 1000 in sendMessage WM_USER + 1000
Try
To obtain the time GetTickCount
Repeat
If GetMessage (Msg, 0, 0, 0) then
The begin
If Msg, Msg=WM_USER + 1000 then
The begin
To get a time lag
end;
End the else
The begin
If Msg, Msg=WM_QUIT then
The begin
ExitProcess (0);
Exit;
end;
end;
Time until (to)
The finally
//off timer
end;
end;

CodePudding user response:

Correction:

Procedure Delay (i_Msc: integer);
Var
Msg: TMsg;
The begin
//start a timer, timer interval, 1000 in sendMessage WM_USER + 1000
Try
To obtain the time GetTickCount
Repeat
If GetMessage (Msg, 0, 0, 0) then
The begin
If Msg, Msg=WM_USER + 1000 then
The begin
To get a time lag
End the else
The begin
TransMessage (Msg);
DispatchMessage (Msg);
end;
End the else
The begin
If Msg, Msg=WM_QUIT then
The begin
ExitProcess (0);
Exit;
end;
end;
Time until (to)
The finally
//off timer
end;
end;

CodePudding user response:

Pure fiddle-faddle ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  • Related