Ignore:
Timestamp:
Jan 26, 2011, 2:16:19 PM (14 years ago)
Author:
george
Message:
  • Modified: Not completed thread safe support.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/UMainForm.pas

    r147 r148  
    1111type
    1212
    13   { TTest }
    14 
    15   TTest = class(TCoroutine)
    16     procedure Execute; override;
    17   end;
    18 
    19   { TForm1 }
    20 
    21   TForm1 = class(TForm)
    22     Button1: TButton;
     13  { TMainForm }
     14
     15  TMainForm = class(TForm)
     16    ButtonSchedulerStartStop: TButton;
    2317    Button2: TButton;
    24     Button3: TButton;
    25     Button4: TButton;
    26     Button5: TButton;
    27     Button6: TButton;
     18    ButtonAddWorkers: TButton;
     19    ButtonGetMaxThread: TButton;
     20    ButtonShowThreadId: TButton;
     21    ButtonClearMicroThreads: TButton;
    2822    Label1: TLabel;
    2923    Label2: TLabel;
    3024    Label3: TLabel;
    3125    Label4: TLabel;
     26    Label5: TLabel;
     27    Label6: TLabel;
    3228    ListView1: TListView;
    3329    Memo1: TMemo;
     
    3531    SpinEdit2: TSpinEdit;
    3632    Timer1: TTimer;
    37     procedure Button1Click(Sender: TObject);
     33    procedure ButtonSchedulerStartStopClick(Sender: TObject);
    3834    procedure Button2Click(Sender: TObject);
    39     procedure Button3Click(Sender: TObject);
    40     procedure Button4Click(Sender: TObject);
    41     procedure Button5Click(Sender: TObject);
    42     procedure Button6Click(Sender: TObject);
     35    procedure ButtonAddWorkersClick(Sender: TObject);
     36    procedure ButtonGetMaxThreadClick(Sender: TObject);
     37    procedure ButtonShowThreadIdClick(Sender: TObject);
     38    procedure ButtonClearMicroThreadsClick(Sender: TObject);
    4339    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    4440    procedure FormCreate(Sender: TObject);
    4541    procedure FormDestroy(Sender: TObject);
    4642    procedure ListView1Data(Sender: TObject; Item: TListItem);
     43    procedure SpinEdit2Change(Sender: TObject);
    4744    procedure Timer1Timer(Sender: TObject);
    4845  private
     
    5047  public
    5148    Scheduler: TMicroThreadScheduler;
    52     Test: TTest;
    53   end;
    54 
    55 var
    56   Form1: TForm1;
     49  end;
     50
     51var
     52  MainForm: TMainForm;
    5753
    5854implementation
     
    6056{ TTest }
    6157
    62 procedure TTest.Execute;
    63 var
    64   I: Integer;
    65 begin
    66 //  for I := 0 to 100 do begin
    67     Form1.Memo1.Lines.Add(IntToStr(I));
    68       Sleep(10);
    69       //raise Exception.Create('Test');
    70       Yield;
    71 
    72 //  end;
    73 end;
    74 
    7558{$R *.lfm}
    7659
    77 { TForm1 }
    78 
    79 procedure TForm1.FormCreate(Sender: TObject);
     60{ TMainForm }
     61
     62procedure TMainForm.FormCreate(Sender: TObject);
    8063begin
    8164  Scheduler := TMicroThreadScheduler.Create;
    82   Test := TTest.Create;
    8365  DoubleBuffered := True;
    8466  ListView1.DoubleBuffered := True;
    85 end;
    86 
    87 procedure TForm1.Button1Click(Sender: TObject);
    88 var
    89   I: Integer;
    90 begin
    91   if Button1.Caption = 'Start scheduler' then begin
    92     Button1.Caption := 'Stop scheduler';
     67  Label6.Caption := IntToStr(Scheduler.GetCPUCoreCount);
     68end;
     69
     70procedure TMainForm.ButtonSchedulerStartStopClick(Sender: TObject);
     71var
     72  I: Integer;
     73begin
     74  if ButtonSchedulerStartStop.Caption = 'Start scheduler' then begin
     75    ButtonSchedulerStartStop.Caption := 'Stop scheduler';
    9376    Memo1.Clear;
    94     Scheduler.ThreadPoolSize := SpinEdit2.Value;
    95     Scheduler.Start;
     77    Scheduler.Active := True;
    9678  end else begin
    97     Button1.Caption := 'Start scheduler';
    98     Scheduler.Stop;
    99     Scheduler.ThreadPoolSize := 0;
    100   end;
    101 end;
    102 
    103 procedure TForm1.Button2Click(Sender: TObject);
     79    ButtonSchedulerStartStop.Caption := 'Start scheduler';
     80    Scheduler.Active := False;
     81  end;
     82end;
     83
     84procedure TMainForm.Button2Click(Sender: TObject);
    10485const
    10586  MaxBlock = MaxInt - $f;
     
    154135end;
    155136
    156 procedure TForm1.Button3Click(Sender: TObject);
    157 begin
    158   Test.Invoke;
    159 end;
    160 
    161 procedure TForm1.Button4Click(Sender: TObject);
    162 var
    163   I: Integer;
    164 begin
    165   Scheduler.MicroThreads.Clear;
     137procedure TMainForm.ButtonAddWorkersClick(Sender: TObject);
     138var
     139  I: Integer;
     140begin
     141  //Scheduler.FMicroThreads.Clear;
    166142  for I := 0 to SpinEdit1.Value do
    167143    Scheduler.AddMethod(Worker);
    168144end;
    169145
    170 procedure TForm1.Button5Click(Sender: TObject);
     146procedure TMainForm.ButtonGetMaxThreadClick(Sender: TObject);
    171147var
    172148  NewThread: TThread;
     
    185161end;
    186162
    187 procedure TForm1.Button6Click(Sender: TObject);
     163procedure TMainForm.ButtonShowThreadIdClick(Sender: TObject);
    188164begin
    189165  ShowMessage(IntToStr(GetThreadID));
    190166end;
    191167
    192 procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
    193 begin
    194   Scheduler.Stop;
    195 end;
    196 
    197 procedure TForm1.FormDestroy(Sender: TObject);
    198 begin
    199   Test.Free;
     168procedure TMainForm.ButtonClearMicroThreadsClick(Sender: TObject);
     169begin
     170  try
     171    Scheduler.MicroThreadsLock.Acquire;
     172    Scheduler.MicroThreads.Clear;
     173  finally
     174    Scheduler.MicroThreadsLock.Release;
     175  end;
     176end;
     177
     178procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
     179begin
     180end;
     181
     182procedure TMainForm.FormDestroy(Sender: TObject);
     183begin
    200184  Scheduler.Free;
    201185end;
    202186
    203 procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
     187procedure TMainForm.ListView1Data(Sender: TObject; Item: TListItem);
    204188begin
    205189  try
    206     Scheduler.Lock.Acquire;
     190    Scheduler.MicroThreadsLock.Acquire;
    207191    if Item.Index < Scheduler.MicroThreads.Count then
    208192    with TMicroThread(Scheduler.MicroThreads[Item.Index]) do begin
     
    216200    end;
    217201  finally
    218     Scheduler.Lock.Release;
    219   end;
    220 end;
    221 
    222 procedure TForm1.Timer1Timer(Sender: TObject);
     202    Scheduler.MicroThreadsLock.Release;
     203  end;
     204end;
     205
     206procedure TMainForm.SpinEdit2Change(Sender: TObject);
     207begin
     208  Scheduler.ThreadPoolSize := SpinEdit2.Value;
     209end;
     210
     211procedure TMainForm.Timer1Timer(Sender: TObject);
    223212begin
    224213  ListView1.Items.Count := Scheduler.MicroThreadCount;
     
    229218end;
    230219
    231 procedure TForm1.Worker(MicroThread: TMicroThread);
     220procedure TMainForm.Worker(MicroThread: TMicroThread);
    232221var
    233222  I: Integer;
Note: See TracChangeset for help on using the changeset viewer.