Indy IdThreadComponent thread study
A few days ago in the development of real-time data analysis function module, they found a huge amount of analytical data, especially time-consuming procedures run up a screen suspended animation.
In order to optimize the user experience, using the Indy comes IdThreadComponent control, the perfect solution to this problem.
Below follows IdThreadComponent record on some of the key properties and methods:
1: IdThreadComponent property
Here the main record about Active, Priority and StopMode property.
Active: Default False, when you need to make IdThreadComponent controls to take effect, set to True;
Priority: In order to avoid conflict between threads, select Normal, the system comes with assigning a priority;
StopMode: Here is divided into two kinds Terminate and Suspend, Terminate is forcibly terminated, Suspend to slow down and wait for termination.
2:IdThreadComponent方法
TIdThreadComponent use the Observer pattern, where all events are callbacks.
Generally more time-consuming operation is performed on OnRun in.
Event execution order is as follows:
On Before Execute;
On Before Run;
On Run;
On Stopped;
On After Run;
On After Execute;
3: is illustrated by the following Demo
You need to explain:
In the thread can not directly control on the user interface, because sometimes control is destroyed and the thread is still running, then direct action AV throw an exception, so TIdThreadComponent callback event, if there is use to control, you must first It does judge whether the control is empty.
Interface is as follows:
code show as below:
1 unit uMain; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, RzButton, StdCtrls, RzEdit, IdBaseComponent, IdThreadComponent; 8 9 type 10 TMainFrm = class(TForm) 11 IDTrd: TIdThreadComponent; 12 mmMsg: TRzMemo; 13 btnStart: TRzBitBtn; 14 btnStop: TRzBitBtn; 15 btnTerminal: TRzBitBtn; 16 procedure btnStartClick(Sender: TObject); 17 procedure IDTrdRun (Sender: TIdCustomThreadComponent); 18 procedure IDTrdBeforeExecute (Sender: TIdCustomThreadComponent); 19 procedure IDTrdAfterExecute (Sender: TIdCustomThreadComponent); 20 procedure btnStopClick (Sender: TObject); 21 procedure btnTerminalClick (Sender: TObject); 22 procedure IDTrdAfterRun (Sender: TIdCustomThreadComponent); 23 procedure IDTrdBeforeRun (Sender: TIdCustomThreadComponent); 24 procedure IDTrdStopped (Sender: TIdCustomThreadComponent); 25 private 26 { Private declarations } 27 public 28 { Public declarations } 29 end; 30 31 var 32 MainFrm: TMainFrm; 33 34 implementation 35 36 {$R *.dfm} 37 38 procedure TMainFrm.btnStartClick(Sender: TObject); 39 begin 40 IDTrd.Active := True; 41 end; 42 43 procedure TMainFrm.IDTrdRun(Sender: TIdCustomThreadComponent); 44 var 45 the I: Integer; 46 is the begin 47 for the I: = . 1 to 100 do 48 the begin 49 mmMsg.Lines.Add (the IntToStr (the I)); // analog consuming operations 50 End ; 51 is // not operate directly in the thread interface controls, and sometimes control is destroyed and the thread is still running, 52 // this time will be reported abnormal AV, 53 // so have to use controls, must be added to determine whether the control is empty callback event in TIdThreadComponent 54 End ; 55 56 is Procedure TMainFrm.IDTrdBeforeExecute (Sender: TIdCustomThreadComponent); 57 is the begin 58 IF Assigned(mmMsg) then 59 begin 60 mmMsg.Lines.Add('Before Execute'); 61 end; 62 end; 63 64 procedure TMainFrm.IDTrdAfterExecute(Sender: TIdCustomThreadComponent); 65 begin 66 if Assigned(mmMsg) then 67 begin 68 mmMsg.Lines.Add('After Execute'); 69 end; 70 end; 71 72 procedure TMainFrm.btnStopClick(Sender: TObject); 73 begin 74 IDTrd.Stop; 75 end; 76 77 procedure TMainFrm.btnTerminalClick(Sender: TObject); 78 begin 79 IDTrd.Terminate; 80 end; 81 82 procedure TMainFrm.IDTrdAfterRun(Sender: TIdCustomThreadComponent); 83 begin 84 if Assigned(mmMsg) then 85 begin 86 mmMsg.Lines.Add('After Run'); 87 end; 88 end; 89 90 procedure TMainFrm.IDTrdBeforeRun(Sender: TIdCustomThreadComponent); 91 begin 92 if Assigned(mmMsg) then 93 begin 94 mmMsg.Lines.Add('Before Run'); 95 end; 96 end; 97 98 procedure TMainFrm.IDTrdStopped(Sender: TIdCustomThreadComponent); 99 begin 100 if Assigned(mmMsg) then 101 begin 102 mmMsg.Lines.Add('On Stopped'); 103 end; 104 end; 105 106 end.