Ignore:
Timestamp:
Jan 28, 2011, 9:10:16 PM (14 years ago)
Author:
george
Message:
  • Fixed: WaitForEvent functionality. Now TEvent.WaitFor will chane microthread to blocked state with defined timeout. TEvent.SetSignal method will unblock all waiting microthreads.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/UMainForm.pas

    r157 r158  
    1111type
    1212
     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
    1324  { TMainForm }
    1425
     
    1627    Button1: TButton;
    1728    Button2: TButton;
     29    Button3: TButton;
    1830    ButtonAddWorkers: TButton;
    1931    ButtonClearMicroThreads: TButton;
     
    2133    ButtonSchedulerStartStop: TButton;
    2234    ButtonShowThreadId: TButton;
     35    CheckBox1: TCheckBox;
     36    CheckBox2: TCheckBox;
     37    CheckBox3: TCheckBox;
    2338    CheckBoxUseMainThread: TCheckBox;
    2439    GroupBox1: TGroupBox;
    2540    GroupBox2: TGroupBox;
     41    GroupBox3: TGroupBox;
    2642    Label1: TLabel;
    2743    Label10: TLabel;
     
    2945    Label12: TLabel;
    3046    Label13: TLabel;
     47    Label14: TLabel;
     48    Label15: TLabel;
     49    Label16: TLabel;
     50    Label17: TLabel;
    3151    Label2: TLabel;
    3252    Label3: TLabel;
     
    4464    SpinEdit2: TSpinEdit;
    4565    SpinEdit3: TSpinEdit;
     66    SpinEdit4: TSpinEdit;
     67    SpinEdit5: TSpinEdit;
    4668    TabSheet1: TTabSheet;
    4769    TabSheet2: TTabSheet;
     
    4971    TimerRedraw: TTimer;
    5072    procedure Button1Click(Sender: TObject);
     73    procedure Button3Click(Sender: TObject);
    5174    procedure ButtonSchedulerStartStopClick(Sender: TObject);
    5275    procedure Button2Click(Sender: TObject);
     
    5578    procedure ButtonShowThreadIdClick(Sender: TObject);
    5679    procedure ButtonClearMicroThreadsClick(Sender: TObject);
     80    procedure CheckBox1Change(Sender: TObject);
     81    procedure CheckBox2Change(Sender: TObject);
     82    procedure CheckBox3Change(Sender: TObject);
    5783    procedure CheckBoxUseMainThreadChange(Sender: TObject);
    5884    procedure FormCreate(Sender: TObject);
     
    6389    procedure SpinEdit2Change(Sender: TObject);
    6490    procedure SpinEdit3Change(Sender: TObject);
     91    procedure SpinEdit5Change(Sender: TObject);
    6592    procedure TimerRedrawTimer(Sender: TObject);
    6693  private
    67     procedure Worker(MicroThread: TMicroThread);
    68     procedure WorkerDoWrite;
    6994    procedure WorkerSubRoutine;
    7095  public
     96    DoWriteToMemo: Boolean;
     97    DoSleep: Boolean;
     98    SleepDuration: Integer;
     99    DoWaitForEvent: Boolean;
     100    Event: TMicroThreadEvent;
     101    WaitForEventDuration: Integer;
    71102    Iterations: Integer;
    72103  end;
     
    88119  ListView1.DoubleBuffered := True;
    89120  Label6.Caption := IntToStr(GetLogicalProcessorCount);
     121  Event := TMicroThreadEvent.Create;
    90122end;
    91123
     
    107139begin
    108140  Memo1.Clear;
     141end;
     142
     143procedure TMainForm.Button3Click(Sender: TObject);
     144begin
     145  Event.SetEvent;
    109146end;
    110147
     
    165202var
    166203  I: Integer;
     204  NewWorker: TWorker;
    167205begin
    168206  //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;
    171212end;
    172213
     
    203244end;
    204245
     246procedure TMainForm.CheckBox1Change(Sender: TObject);
     247begin
     248  SleepDuration := SpinEdit4.Value;
     249  DoSleep := CheckBox1.Checked;
     250end;
     251
     252procedure TMainForm.CheckBox2Change(Sender: TObject);
     253begin
     254  DoWriteToMemo := CheckBox2.Checked;
     255end;
     256
     257procedure TMainForm.CheckBox3Change(Sender: TObject);
     258begin
     259  DoWaitForEvent := CheckBox3.Checked;
     260  WaitForEventDuration := SpinEdit5.Value;
     261end;
     262
    205263procedure TMainForm.CheckBoxUseMainThreadChange(Sender: TObject);
    206264begin
     
    211269begin
    212270  MainScheduler.Active := False;
     271  Event.Free;
    213272end;
    214273
     
    216275begin
    217276  Iterations := SpinEdit3.Value;
    218   SpinEdit2.Value := 20;
     277  SpinEdit2.Value := 6;
    219278  ButtonAddWorkers.Click;
    220279  ButtonSchedulerStartStop.Click;
     280  Label16.Caption := IntToStr(MainThreadID);
    221281end;
    222282
     
    267327end;
    268328
     329procedure TMainForm.SpinEdit5Change(Sender: TObject);
     330begin
     331
     332end;
     333
    269334procedure TMainForm.TimerRedrawTimer(Sender: TObject);
    270335begin
     
    290355end;
    291356
    292 procedure TMainForm.Worker(MicroThread: TMicroThread);
     357procedure TWorker.Execute;
    293358var
    294359  I: Integer;
    295360  Q: Integer;
    296361begin
    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;
     372end;
     373
     374procedure TWorker.DoWriteToMemo;
     375begin
     376  MainForm.Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(Trunc(Completion * 100)) + ' %');
    316377end;
    317378
Note: See TracChangeset for help on using the changeset viewer.