Ignore:
Timestamp:
Jan 23, 2011, 8:04:49 PM (14 years ago)
Author:
george
Message:
  • Added: Showing MicroThread list.
  • Added: Measuring execution time.
  • Added: Do not free micro thread on finish flag.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Microthreading/Demo/umainform.pas

    r134 r135  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   ComCtrls, UMicroThreading, Coroutine;
     9  ComCtrls, ExtCtrls, UMicroThreading, Coroutine, DateUtils;
    1010
    1111type
     
    2626    ListView1: TListView;
    2727    Memo1: TMemo;
     28    Timer1: TTimer;
    2829    procedure Button1Click(Sender: TObject);
    2930    procedure Button2Click(Sender: TObject);
     
    3132    procedure FormCreate(Sender: TObject);
    3233    procedure FormDestroy(Sender: TObject);
     34    procedure Timer1Timer(Sender: TObject);
    3335  private
    3436    procedure Worker(MicroThread: TMicroThread);
     
    6567begin
    6668  Scheduler := TMicroThreadScheduler.Create;
     69  Scheduler.FreeMicroThreadOnFinish := False;
    6770  Test := TTest.Create;
    6871end;
     
    7477  for I := 0 to 1 do
    7578    Scheduler.Add('Worker', Worker);
    76   Scheduler.Start;
     79  repeat
     80    Scheduler.Start;
     81    Application.ProcessMessages;
     82    Sleep(1);
     83  until Scheduler.MicroThreadCount = 0;
    7784end;
    7885
     
    141148end;
    142149
     150procedure TForm1.Timer1Timer(Sender: TObject);
     151var
     152  I: Integer;
     153  NewItem: TListItem;
     154begin
     155  try
     156    ListView1.BeginUpdate;
     157    ListView1.Clear;
     158    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));
     168    end;
     169  finally
     170    Scheduler.Lock.Release;
     171    ListView1.EndUpdate;
     172  end;
     173end;
     174
    143175procedure TForm1.Worker(MicroThread: TMicroThread);
    144176var
     
    149181    for I := 0 to 10 do begin
    150182      Memo1.Lines.Add(InttoStr(Id) + ': ' + IntToStr(I));
    151       SysUtils.Sleep(10 * Id);
    152       Yield;
     183      Sleep(100 * Id * OneMillisecond);
    153184    end;
    154185  end;
Note: See TracChangeset for help on using the changeset viewer.