Changeset 162 for MicroThreading/Demo/UMainForm.pas
- Timestamp:
- Feb 7, 2011, 1:04:27 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo/UMainForm.pas
r161 r162 20 20 MainForm: TMainForm; 21 21 procedure DoWriteToMemo; 22 constructor Create(CreateSuspended: Boolean; 23 const StackSize: SizeUInt = DefaultStackSize); 24 destructor Destroy; override; 22 25 end; 23 26 … … 38 41 CheckBox2: TCheckBox; 39 42 CheckBox3: TCheckBox; 43 CheckBox4: TCheckBox; 40 44 CheckBoxUseMainThread: TCheckBox; 41 45 GroupBox1: TGroupBox; … … 45 49 Label11: TLabel; 46 50 Label12: TLabel; 47 Label13: TLabel;48 51 Label14: TLabel; 49 52 Label15: TLabel; 50 53 Label16: TLabel; 51 54 Label17: TLabel; 55 Label18: TLabel; 52 56 Label2: TLabel; 53 57 Label3: TLabel; … … 58 62 Label8: TLabel; 59 63 Label9: TLabel; 60 ListView2: TListView;61 64 Memo1: TMemo; 62 65 PageControl1: TPageControl; … … 66 69 SpinEdit4: TSpinEdit; 67 70 SpinEdit5: TSpinEdit; 71 SpinEdit6: TSpinEdit; 68 72 TabSheet1: TTabSheet; 69 73 TabSheet2: TTabSheet; … … 83 87 procedure CheckBox2Change(Sender: TObject); 84 88 procedure CheckBox3Change(Sender: TObject); 89 procedure CheckBox4Change(Sender: TObject); 85 90 procedure CheckBoxUseMainThreadChange(Sender: TObject); 86 91 procedure FormCreate(Sender: TObject); … … 91 96 procedure SpinEdit3Change(Sender: TObject); 92 97 procedure SpinEdit5Change(Sender: TObject); 98 procedure SpinEdit6Change(Sender: TObject); 93 99 procedure TimerRedrawTimer(Sender: TObject); 94 100 private 95 101 MicroThreadList: TMicroThreadList; 102 Lock: TMicroThreadCriticalSection; 96 103 LastException: Exception; 97 104 LastExceptionSender: TObject; … … 102 109 DoWriteToMemo: Boolean; 103 110 DoSleep: Boolean; 111 DoCriticalSection: Boolean; 104 112 RaiseException: Boolean; 105 113 SleepDuration: Integer; 114 CriticalSectionSleepDuration: Integer; 106 115 DoWaitForEvent: Boolean; 107 116 Event: TMicroThreadEvent; … … 129 138 MicroThreadList := TMicroThreadList.Create(Self); 130 139 UMicroThreading.ExceptionHandler := ShowException; 140 Lock := TMicroThreadCriticalSection.Create; 131 141 end; 132 142 … … 280 290 end; 281 291 292 procedure TMainForm.CheckBox4Change(Sender: TObject); 293 begin 294 CriticalSectionSleepDuration := SpinEdit4.Value; 295 DoCriticalSection := CheckBox4.Checked; 296 end; 297 282 298 procedure TMainForm.CheckBoxUseMainThreadChange(Sender: TObject); 283 299 begin … … 290 306 MainScheduler.Active := False; 291 307 Event.Free; 308 Lock.Free; 292 309 end; 293 310 … … 303 320 procedure TMainForm.ListView2Data(Sender: TObject; Item: TListItem); 304 321 begin 305 if Item.Index < MainScheduler.ThreadPoolCount then306 try307 MainScheduler.ThreadPoolLock.Acquire;308 with TMicroThreadThread(MainScheduler.ThreadPool[Item.Index]) do begin309 Item.Caption := IntToStr(ThreadID);310 Item.SubItems.Add(MicroThreadThreadStateText[State]);311 end;312 finally313 MainScheduler.ThreadPoolLock.Release;314 end;315 322 end; 316 323 … … 330 337 end; 331 338 339 procedure TMainForm.SpinEdit6Change(Sender: TObject); 340 begin 341 end; 342 332 343 procedure TMainForm.TimerRedrawTimer(Sender: TObject); 333 344 begin 334 if ListView2.Items.Count <> MainScheduler.ThreadPoolCount then335 ListView2.Items.Count := MainScheduler.ThreadPoolCount;336 ListView2.Items[-1];337 ListView2.Refresh;338 339 345 Label2.Caption := DateTimeToStr(NowPrecise) + ' ' + 340 346 FloatToStr(Frac(NowPrecise / OneSecond)); … … 369 375 begin 370 376 for I := 0 to MainForm.Iterations - 1 do begin 371 try372 377 Q := 0; 373 while Q < 100 do Inc(Q);378 while Q < 100000 do Inc(Q); 374 379 if MainForm.DoWriteToMemo then Synchronize(DoWriteToMemo); 375 380 if MainForm.DoWaitForEvent then MainForm.Event.WaitFor(MainForm.WaitForEventDuration * OneMillisecond); … … 379 384 raise Exception.Create('Exception from microthread'); 380 385 end; 386 if MainForm.DoCriticalSection then begin 387 try 388 MainForm.Lock.Acquire; 389 MTSleep(MainForm.CriticalSectionSleepDuration * OneMillisecond); 390 finally 391 MainForm.Lock.Release; 392 end; 393 end; 381 394 //WorkerSubRoutine; 382 395 Completion := I / MainForm.Iterations; 383 396 Yield; 384 385 except386 Q := 0;387 raise Exception.Create('Exception from microthread');388 end;389 397 end; 390 398 end; … … 395 403 end; 396 404 405 constructor TWorker.Create(CreateSuspended: Boolean; const StackSize: SizeUInt); 406 begin 407 inherited; 408 end; 409 410 destructor TWorker.Destroy; 411 begin 412 inherited Destroy; 413 end; 414 397 415 end. 398 416
Note:
See TracChangeset
for help on using the changeset viewer.