Ignore:
Timestamp:
Jan 24, 2011, 8:39:52 AM (14 years ago)
Author:
george
Message:
  • Fixed: Execution time measurement.
  • Modified: MicroThread state list changed to virtual list.
Location:
MicroThreading/Demo
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo

    • Property svn:ignore
      •  

        old new  
        11Demo
        22lib
         3Demo.exe
  • MicroThreading/Demo/UMainForm.pas

    r137 r140  
    2424    Button3: TButton;
    2525    Label1: TLabel;
     26    Label2: TLabel;
    2627    ListView1: TListView;
    2728    Memo1: TMemo;
     
    3031    procedure Button2Click(Sender: TObject);
    3132    procedure Button3Click(Sender: TObject);
     33    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    3234    procedure FormCreate(Sender: TObject);
    3335    procedure FormDestroy(Sender: TObject);
     36    procedure ListView1Data(Sender: TObject; Item: TListItem);
    3437    procedure Timer1Timer(Sender: TObject);
    3538  private
     
    3841    Scheduler: TMicroThreadScheduler;
    3942    Test: TTest;
     43    Terminate: Boolean;
    4044  end;
    4145
     
    7478var
    7579  I: Integer;
    76 begin
    77   for I := 0 to 1 do
    78     Scheduler.Add('Worker', Worker);
    79   repeat
    80     Scheduler.Start;
    81     Application.ProcessMessages;
    82     Sleep(1);
    83   until Scheduler.MicroThreadCount = 0;
     80  Executed: Integer;
     81begin
     82  if Button1.Caption = 'Start scheduler' then begin
     83    Button1.Caption := 'Stop scheduler';
     84    Terminate := False;
     85    Scheduler.MicroThreads.Clear;
     86    Memo1.Clear;
     87    for I := 0 to 20 do
     88      Scheduler.Add('Worker', Worker);
     89    repeat
     90      Executed := Scheduler.Execute(10);
     91      Application.ProcessMessages;
     92      if Executed = 0 then Sleep(1);
     93    until (Scheduler.MicroThreadCount = 0) or Terminate;
     94  end else begin
     95    Button1.Caption := 'Start scheduler';
     96    Terminate := True;
     97  end;
    8498end;
    8599
     
    142156end;
    143157
     158procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
     159begin
     160  Terminate := True;
     161end;
     162
    144163procedure TForm1.FormDestroy(Sender: TObject);
    145164begin
     
    148167end;
    149168
    150 procedure TForm1.Timer1Timer(Sender: TObject);
    151 var
    152   I: Integer;
    153   NewItem: TListItem;
     169procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
    154170begin
    155171  try
    156     ListView1.BeginUpdate;
    157     ListView1.Clear;
    158172    Scheduler.Lock.Acquire;
    159     for I := 0 to Scheduler.MicroThreads.Count - 1 do
    160     with TMicroThread(Scheduler.MicroThreads[I]) do begin
    161       NewItem := ListView1.Items.Add;
    162       NewItem.Caption := IntToStr(Id);
    163       NewItem.SubItems.Add(Name);
    164       NewItem.SubItems.Add('');
    165       NewItem.SubItems.Add(IntToStr(Priority));
    166       NewItem.SubItems.Add(MicroThreadStateText[State]);
    167       NewItem.SubItems.Add(FloatToStr(ExecutionTime));
     173    if Item.Index < Scheduler.MicroThreads.Count then
     174    with TMicroThread(Scheduler.MicroThreads[Item.Index]) do begin
     175      Item.Caption := IntToStr(Id);
     176      Item.SubItems.Add(Name);
     177      Item.SubItems.Add('');
     178      Item.SubItems.Add(IntToStr(Priority));
     179      Item.SubItems.Add(MicroThreadStateText[State]);
     180      Item.SubItems.Add(FloatToStr(ExecutionTime));
    168181    end;
    169182  finally
    170183    Scheduler.Lock.Release;
    171     ListView1.EndUpdate;
    172   end;
     184  end;
     185end;
     186
     187procedure TForm1.Timer1Timer(Sender: TObject);
     188begin
     189  ListView1.Items.Count := Scheduler.MicroThreadCount;
     190  ListView1.Items[-1];
     191  ListView1.Refresh;
     192  Label2.Caption := DateTimeToStr(Scheduler.GetNow) + ' ' +
     193    FloatToStr(Frac(Scheduler.GetNow / OneSecond));
    173194end;
    174195
     
    179200  with MicroThread do begin
    180201    Memo1.Lines.Add('Worker ' + IntToStr(Id));
    181     for I := 0 to 10 do begin
    182       Memo1.Lines.Add(InttoStr(Id) + ': ' + IntToStr(I));
    183       Sleep(100 * Id * OneMillisecond);
     202    for I := 0 to 1000 do begin
     203      Memo1.Lines.Add(IntToStr(Id) + ': ' + IntToStr(I) + ' ' +
     204        FloatToStr(ExecutionTime));
     205      //Sleep(1 * Id * OneMillisecond);
     206      Yield;
    184207    end;
    185208  end;
Note: See TracChangeset for help on using the changeset viewer.