The unit Unit1;
Interface
USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls ExtCtrls;
Type
TForm1=class (TForm)
For: TButton;
Button2: TButton;
Button3: TButton;
Procedure Button1Click (Sender: TObject);
Procedure FormCreate (Sender: TObject);
Procedure Button2Click (Sender: TObject);
Procedure Button3Click (Sender: TObject);
Procedure FormClose (Sender: TObject; Var Action: TCloseAction);
Private
{Private declarations}
Public
{Public declarations}
end;
Type
TMyThread=class (TThread)
Private
IMaxNum: Integer;
FTimer: TTimer;
Protected
Procedure the Execute; Override.
Procedure TimerProc (Sender: TObject);
end;
Var
Form1: TForm1;
Editm: array [0.. 40] of tedit; I, iX, iy: Integer;
MyThread: array [0.. 40] of TMyThread;
Implementation
{$R *. DFM}
Procedure TMyThread. Execute;
Var
I: Integer; Msg: TMsg;
The begin
FreeOnTerminate:=True; {this allows threads execute immediately after the release}
FTimer:=TTimer. Create (nil);
Try
FTimer. Enabled:=True;
FTimer. Interval:=1000;
FTimer. OnTimer:=TimerProc;
While GetMessage (Msg, 0, 0, 0) do
The begin
The TranslateMessage (Msg);
The DispatchMessage (Msg)
end;
The Terminate;
The finally
FTimer. Free;
end;
end;
Procedure TMyThread. TimerProc (Sender: TObject);
Var I: Integer;
The begin
For I:=0 to 1000 do
The begin
Editm [iMaxNum]. Text:=IntToStr (I);
end;
end;
Procedure TForm1. Button1Click (Sender: TObject);
Var I: Integer;
The begin
For I:=0 to 40 do
The begin
MyThread [I]. Resume;
end;
end;
Procedure TForm1. FormCreate (Sender: TObject);
The begin
Ix:=0; Iy:=0;
For I:=0 to 40 do
The begin
If iX> 10 then
Ix:=0;
Editm [I] :=tedit the Create (self);
The Parent editm [I]=form1;
Editm [I]. Width:=120;
Editm [I]. Height:=20;
Editm [I]. Left:=ix * 120;
Top editm [I]=trunc (iy/5) x 25;
Editm [I]. Name:='edit' + inttostr (I);
Text editm [I]='edit' + inttostr (I);
Visible editm [I]=true;
Inc (iX);
Inc (iy);
MyThread [I] :=TMyThread. Create (True);
IMaxNum MyThread [I]=I;
end;
end;
//hung thread
Procedure TForm1. Button2Click (Sender: TObject);
Var I: Integer;
The begin
For I:=0 to 40 do
The begin
MyThread [I]. Suspend;
end;
end;
//end thread
Procedure TForm1. Button3Click (Sender: TObject);
Var I: Integer;
The begin
For I:=0 to 40 do
The begin
MyThread [I] the Terminate;
end;
end;
Procedure TForm1. FormClose (Sender: TObject; Var Action: TCloseAction);
The begin
Button2. Click;
Button3. Click;
end;
end.
CodePudding user response:
No output to the CPU as shown on the edit all could not see, so long as not all show that won't slowCodePudding user response:
TTimer controls, itself is a time step into separate threads, thread is unfavorable use TTimer control, otherwise very slow, if time in the thread, by reading the system time processing is better,CodePudding user response:
Remove the news cycle that first, into dead circulation, refresh the interface in the main thread, so the edit operation to write in a process, use the Synchronize method calls, program created 41 threads, thread to create the timer, thread completes the timer immediately release, the thread is destroyed, the timer can't continue to work, there is no need to use, so the entire program is one of the most refreshing interface, it completed in the main thread, also does not have a large amount of calculation, you couldn't see the necessity of using multithreading,CodePudding user response:
Recent studies of Einstein's theory of relativity, the faster the speed, time will swell, time will slow down,Multithreading is faster than the single process, so this is normal
CodePudding user response:
The Timer control using a message processing, you in the thread running clock is meaningless! All threads, then it is good to use thread to analog clock, process the data in the background, processing after sending an Event signal, then it is good to the front desk to refresh the UI, reasonable use the Event to handleCodePudding user response:
You are either made multithreaded BMDThread controls, very simple to useCodePudding user response:
Paging way to realize it,