Changeset 140 for MicroThreading/Demo/UMainForm.pas
- Timestamp:
- Jan 24, 2011, 8:39:52 AM (14 years ago)
- Location:
- MicroThreading/Demo
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
MicroThreading/Demo
- Property svn:ignore
-
old new 1 1 Demo 2 2 lib 3 Demo.exe
-
- Property svn:ignore
-
MicroThreading/Demo/UMainForm.pas
r137 r140 24 24 Button3: TButton; 25 25 Label1: TLabel; 26 Label2: TLabel; 26 27 ListView1: TListView; 27 28 Memo1: TMemo; … … 30 31 procedure Button2Click(Sender: TObject); 31 32 procedure Button3Click(Sender: TObject); 33 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 32 34 procedure FormCreate(Sender: TObject); 33 35 procedure FormDestroy(Sender: TObject); 36 procedure ListView1Data(Sender: TObject; Item: TListItem); 34 37 procedure Timer1Timer(Sender: TObject); 35 38 private … … 38 41 Scheduler: TMicroThreadScheduler; 39 42 Test: TTest; 43 Terminate: Boolean; 40 44 end; 41 45 … … 74 78 var 75 79 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; 81 begin 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; 84 98 end; 85 99 … … 142 156 end; 143 157 158 procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); 159 begin 160 Terminate := True; 161 end; 162 144 163 procedure TForm1.FormDestroy(Sender: TObject); 145 164 begin … … 148 167 end; 149 168 150 procedure TForm1.Timer1Timer(Sender: TObject); 151 var 152 I: Integer; 153 NewItem: TListItem; 169 procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem); 154 170 begin 155 171 try 156 ListView1.BeginUpdate;157 ListView1.Clear;158 172 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)); 168 181 end; 169 182 finally 170 183 Scheduler.Lock.Release; 171 ListView1.EndUpdate; 172 end; 184 end; 185 end; 186 187 procedure TForm1.Timer1Timer(Sender: TObject); 188 begin 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)); 173 194 end; 174 195 … … 179 200 with MicroThread do begin 180 201 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; 184 207 end; 185 208 end;
Note:
See TracChangeset
for help on using the changeset viewer.