Changeset 158 for MicroThreading/Demo/UMainForm.pas
- Timestamp:
- Jan 28, 2011, 9:10:16 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/UMainForm.pas
r157 r158 11 11 type 12 12 13 TMainForm = class; 14 15 { TWorker } 16 17 TWorker = class(TMicroThread) 18 procedure Execute; override; 19 private 20 MainForm: TMainForm; 21 procedure DoWriteToMemo; 22 end; 23 13 24 { TMainForm } 14 25 … … 16 27 Button1: TButton; 17 28 Button2: TButton; 29 Button3: TButton; 18 30 ButtonAddWorkers: TButton; 19 31 ButtonClearMicroThreads: TButton; … … 21 33 ButtonSchedulerStartStop: TButton; 22 34 ButtonShowThreadId: TButton; 35 CheckBox1: TCheckBox; 36 CheckBox2: TCheckBox; 37 CheckBox3: TCheckBox; 23 38 CheckBoxUseMainThread: TCheckBox; 24 39 GroupBox1: TGroupBox; 25 40 GroupBox2: TGroupBox; 41 GroupBox3: TGroupBox; 26 42 Label1: TLabel; 27 43 Label10: TLabel; … … 29 45 Label12: TLabel; 30 46 Label13: TLabel; 47 Label14: TLabel; 48 Label15: TLabel; 49 Label16: TLabel; 50 Label17: TLabel; 31 51 Label2: TLabel; 32 52 Label3: TLabel; … … 44 64 SpinEdit2: TSpinEdit; 45 65 SpinEdit3: TSpinEdit; 66 SpinEdit4: TSpinEdit; 67 SpinEdit5: TSpinEdit; 46 68 TabSheet1: TTabSheet; 47 69 TabSheet2: TTabSheet; … … 49 71 TimerRedraw: TTimer; 50 72 procedure Button1Click(Sender: TObject); 73 procedure Button3Click(Sender: TObject); 51 74 procedure ButtonSchedulerStartStopClick(Sender: TObject); 52 75 procedure Button2Click(Sender: TObject); … … 55 78 procedure ButtonShowThreadIdClick(Sender: TObject); 56 79 procedure ButtonClearMicroThreadsClick(Sender: TObject); 80 procedure CheckBox1Change(Sender: TObject); 81 procedure CheckBox2Change(Sender: TObject); 82 procedure CheckBox3Change(Sender: TObject); 57 83 procedure CheckBoxUseMainThreadChange(Sender: TObject); 58 84 procedure FormCreate(Sender: TObject); … … 63 89 procedure SpinEdit2Change(Sender: TObject); 64 90 procedure SpinEdit3Change(Sender: TObject); 91 procedure SpinEdit5Change(Sender: TObject); 65 92 procedure TimerRedrawTimer(Sender: TObject); 66 93 private 67 procedure Worker(MicroThread: TMicroThread);68 procedure WorkerDoWrite;69 94 procedure WorkerSubRoutine; 70 95 public 96 DoWriteToMemo: Boolean; 97 DoSleep: Boolean; 98 SleepDuration: Integer; 99 DoWaitForEvent: Boolean; 100 Event: TMicroThreadEvent; 101 WaitForEventDuration: Integer; 71 102 Iterations: Integer; 72 103 end; … … 88 119 ListView1.DoubleBuffered := True; 89 120 Label6.Caption := IntToStr(GetLogicalProcessorCount); 121 Event := TMicroThreadEvent.Create; 90 122 end; 91 123 … … 107 139 begin 108 140 Memo1.Clear; 141 end; 142 143 procedure TMainForm.Button3Click(Sender: TObject); 144 begin 145 Event.SetEvent; 109 146 end; 110 147 … … 165 202 var 166 203 I: Integer; 204 NewWorker: TWorker; 167 205 begin 168 206 //Scheduler.FMicroThreads.Clear; 169 for I := 0 to SpinEdit1.Value - 1 do 170 MainScheduler.AddMethod(Worker); 207 for I := 0 to SpinEdit1.Value - 1 do begin 208 NewWorker := TWorker.Create(True); 209 NewWorker.MainForm := Self; 210 NewWorker.Start; 211 end; 171 212 end; 172 213 … … 203 244 end; 204 245 246 procedure TMainForm.CheckBox1Change(Sender: TObject); 247 begin 248 SleepDuration := SpinEdit4.Value; 249 DoSleep := CheckBox1.Checked; 250 end; 251 252 procedure TMainForm.CheckBox2Change(Sender: TObject); 253 begin 254 DoWriteToMemo := CheckBox2.Checked; 255 end; 256 257 procedure TMainForm.CheckBox3Change(Sender: TObject); 258 begin 259 DoWaitForEvent := CheckBox3.Checked; 260 WaitForEventDuration := SpinEdit5.Value; 261 end; 262 205 263 procedure TMainForm.CheckBoxUseMainThreadChange(Sender: TObject); 206 264 begin … … 211 269 begin 212 270 MainScheduler.Active := False; 271 Event.Free; 213 272 end; 214 273 … … 216 275 begin 217 276 Iterations := SpinEdit3.Value; 218 SpinEdit2.Value := 20;277 SpinEdit2.Value := 6; 219 278 ButtonAddWorkers.Click; 220 279 ButtonSchedulerStartStop.Click; 280 Label16.Caption := IntToStr(MainThreadID); 221 281 end; 222 282 … … 267 327 end; 268 328 329 procedure TMainForm.SpinEdit5Change(Sender: TObject); 330 begin 331 332 end; 333 269 334 procedure TMainForm.TimerRedrawTimer(Sender: TObject); 270 335 begin … … 290 355 end; 291 356 292 procedure T MainForm.Worker(MicroThread: TMicroThread);357 procedure TWorker.Execute; 293 358 var 294 359 I: Integer; 295 360 Q: Integer; 296 361 begin 297 with MicroThread do begin 298 //Memo1.Lines.Add('Worker ' + IntToStr(Id)); 299 for I := 0 to Iterations - 1 do begin 300 Q := 0; 301 while Q < 100 do Inc(Q); 302 //Synchronize(WorkerDoWrite); 303 //Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' + 304 // FloatToStr(ExecutionTime)); 305 Completion := I / Iterations; 306 //MTSleep(1000 * OneMillisecond); 307 Yield; 308 //WorkerSubRoutine; 309 end; 310 end; 311 end; 312 313 procedure TMainForm.WorkerDoWrite; 314 begin 315 //Memo1.Lines.Add('.'); 362 for I := 0 to MainForm.Iterations - 1 do begin 363 Q := 0; 364 while Q < 100 do Inc(Q); 365 if MainForm.DoWriteToMemo then Synchronize(DoWriteToMemo); 366 if MainForm.DoWaitForEvent then MainForm.Event.WaitFor(MainForm.WaitForEventDuration * OneMillisecond); 367 if MainForm.DoSleep then MTSleep(MainForm.SleepDuration * OneMillisecond); 368 //WorkerSubRoutine; 369 Completion := I / MainForm.Iterations; 370 Yield; 371 end; 372 end; 373 374 procedure TWorker.DoWriteToMemo; 375 begin 376 MainForm.Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(Trunc(Completion * 100)) + ' %'); 316 377 end; 317 378
Note:
See TracChangeset
for help on using the changeset viewer.