Changeset 148 for MicroThreading/Demo/UMainForm.pas
- Timestamp:
- Jan 26, 2011, 2:16:19 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/UMainForm.pas
r147 r148 11 11 type 12 12 13 { TTest } 14 15 TTest = class(TCoroutine) 16 procedure Execute; override; 17 end; 18 19 { TForm1 } 20 21 TForm1 = class(TForm) 22 Button1: TButton; 13 { TMainForm } 14 15 TMainForm = class(TForm) 16 ButtonSchedulerStartStop: TButton; 23 17 Button2: TButton; 24 Button 3: TButton;25 Button 4: TButton;26 Button 5: TButton;27 Button 6: TButton;18 ButtonAddWorkers: TButton; 19 ButtonGetMaxThread: TButton; 20 ButtonShowThreadId: TButton; 21 ButtonClearMicroThreads: TButton; 28 22 Label1: TLabel; 29 23 Label2: TLabel; 30 24 Label3: TLabel; 31 25 Label4: TLabel; 26 Label5: TLabel; 27 Label6: TLabel; 32 28 ListView1: TListView; 33 29 Memo1: TMemo; … … 35 31 SpinEdit2: TSpinEdit; 36 32 Timer1: TTimer; 37 procedure Button 1Click(Sender: TObject);33 procedure ButtonSchedulerStartStopClick(Sender: TObject); 38 34 procedure Button2Click(Sender: TObject); 39 procedure Button 3Click(Sender: TObject);40 procedure Button 4Click(Sender: TObject);41 procedure Button 5Click(Sender: TObject);42 procedure Button 6Click(Sender: TObject);35 procedure ButtonAddWorkersClick(Sender: TObject); 36 procedure ButtonGetMaxThreadClick(Sender: TObject); 37 procedure ButtonShowThreadIdClick(Sender: TObject); 38 procedure ButtonClearMicroThreadsClick(Sender: TObject); 43 39 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 44 40 procedure FormCreate(Sender: TObject); 45 41 procedure FormDestroy(Sender: TObject); 46 42 procedure ListView1Data(Sender: TObject; Item: TListItem); 43 procedure SpinEdit2Change(Sender: TObject); 47 44 procedure Timer1Timer(Sender: TObject); 48 45 private … … 50 47 public 51 48 Scheduler: TMicroThreadScheduler; 52 Test: TTest; 53 end; 54 55 var 56 Form1: TForm1; 49 end; 50 51 var 52 MainForm: TMainForm; 57 53 58 54 implementation … … 60 56 { TTest } 61 57 62 procedure TTest.Execute;63 var64 I: Integer;65 begin66 // for I := 0 to 100 do begin67 Form1.Memo1.Lines.Add(IntToStr(I));68 Sleep(10);69 //raise Exception.Create('Test');70 Yield;71 72 // end;73 end;74 75 58 {$R *.lfm} 76 59 77 { T Form1}78 79 procedure T Form1.FormCreate(Sender: TObject);60 { TMainForm } 61 62 procedure TMainForm.FormCreate(Sender: TObject); 80 63 begin 81 64 Scheduler := TMicroThreadScheduler.Create; 82 Test := TTest.Create;83 65 DoubleBuffered := True; 84 66 ListView1.DoubleBuffered := True; 85 end; 86 87 procedure TForm1.Button1Click(Sender: TObject); 88 var 89 I: Integer; 90 begin 91 if Button1.Caption = 'Start scheduler' then begin 92 Button1.Caption := 'Stop scheduler'; 67 Label6.Caption := IntToStr(Scheduler.GetCPUCoreCount); 68 end; 69 70 procedure TMainForm.ButtonSchedulerStartStopClick(Sender: TObject); 71 var 72 I: Integer; 73 begin 74 if ButtonSchedulerStartStop.Caption = 'Start scheduler' then begin 75 ButtonSchedulerStartStop.Caption := 'Stop scheduler'; 93 76 Memo1.Clear; 94 Scheduler.ThreadPoolSize := SpinEdit2.Value; 95 Scheduler.Start; 77 Scheduler.Active := True; 96 78 end else begin 97 Button1.Caption := 'Start scheduler'; 98 Scheduler.Stop; 99 Scheduler.ThreadPoolSize := 0; 100 end; 101 end; 102 103 procedure TForm1.Button2Click(Sender: TObject); 79 ButtonSchedulerStartStop.Caption := 'Start scheduler'; 80 Scheduler.Active := False; 81 end; 82 end; 83 84 procedure TMainForm.Button2Click(Sender: TObject); 104 85 const 105 86 MaxBlock = MaxInt - $f; … … 154 135 end; 155 136 156 procedure TForm1.Button3Click(Sender: TObject); 157 begin 158 Test.Invoke; 159 end; 160 161 procedure TForm1.Button4Click(Sender: TObject); 162 var 163 I: Integer; 164 begin 165 Scheduler.MicroThreads.Clear; 137 procedure TMainForm.ButtonAddWorkersClick(Sender: TObject); 138 var 139 I: Integer; 140 begin 141 //Scheduler.FMicroThreads.Clear; 166 142 for I := 0 to SpinEdit1.Value do 167 143 Scheduler.AddMethod(Worker); 168 144 end; 169 145 170 procedure T Form1.Button5Click(Sender: TObject);146 procedure TMainForm.ButtonGetMaxThreadClick(Sender: TObject); 171 147 var 172 148 NewThread: TThread; … … 185 161 end; 186 162 187 procedure T Form1.Button6Click(Sender: TObject);163 procedure TMainForm.ButtonShowThreadIdClick(Sender: TObject); 188 164 begin 189 165 ShowMessage(IntToStr(GetThreadID)); 190 166 end; 191 167 192 procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); 193 begin 194 Scheduler.Stop; 195 end; 196 197 procedure TForm1.FormDestroy(Sender: TObject); 198 begin 199 Test.Free; 168 procedure TMainForm.ButtonClearMicroThreadsClick(Sender: TObject); 169 begin 170 try 171 Scheduler.MicroThreadsLock.Acquire; 172 Scheduler.MicroThreads.Clear; 173 finally 174 Scheduler.MicroThreadsLock.Release; 175 end; 176 end; 177 178 procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction); 179 begin 180 end; 181 182 procedure TMainForm.FormDestroy(Sender: TObject); 183 begin 200 184 Scheduler.Free; 201 185 end; 202 186 203 procedure T Form1.ListView1Data(Sender: TObject; Item: TListItem);187 procedure TMainForm.ListView1Data(Sender: TObject; Item: TListItem); 204 188 begin 205 189 try 206 Scheduler. Lock.Acquire;190 Scheduler.MicroThreadsLock.Acquire; 207 191 if Item.Index < Scheduler.MicroThreads.Count then 208 192 with TMicroThread(Scheduler.MicroThreads[Item.Index]) do begin … … 216 200 end; 217 201 finally 218 Scheduler.Lock.Release; 219 end; 220 end; 221 222 procedure TForm1.Timer1Timer(Sender: TObject); 202 Scheduler.MicroThreadsLock.Release; 203 end; 204 end; 205 206 procedure TMainForm.SpinEdit2Change(Sender: TObject); 207 begin 208 Scheduler.ThreadPoolSize := SpinEdit2.Value; 209 end; 210 211 procedure TMainForm.Timer1Timer(Sender: TObject); 223 212 begin 224 213 ListView1.Items.Count := Scheduler.MicroThreadCount; … … 229 218 end; 230 219 231 procedure T Form1.Worker(MicroThread: TMicroThread);220 procedure TMainForm.Worker(MicroThread: TMicroThread); 232 221 var 233 222 I: Integer;
Note:
See TracChangeset
for help on using the changeset viewer.