Ignore:
Timestamp:
Feb 9, 2011, 1:35:28 PM (13 years ago)
Author:
george
Message:
  • Modified: Sync functions now not accept main thread context. Instead MainScheduler.AddMethod have to by used.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading/Demo/UMainForm.pas

    r164 r166  
    3333    Button4: TButton;
    3434    Button5: TButton;
     35    Button6: TButton;
    3536    ButtonAddWorkers: TButton;
    3637    ButtonClearMicroThreads: TButton;
     
    7778    procedure Button4Click(Sender: TObject);
    7879    procedure Button5Click(Sender: TObject);
     80    procedure Button6Click(Sender: TObject);
    7981    procedure ButtonSchedulerStartStopClick(Sender: TObject);
    8082    procedure Button2Click(Sender: TObject);
     
    105107    procedure ShowException(Sender: TObject; E: Exception);
    106108    procedure DoShowException;
     109    procedure MethodWorker;
    107110  public
    108111    DoWriteToMemo: Boolean;
     
    171174begin
    172175  RaiseException := True;
     176end;
     177
     178procedure TMainForm.Button6Click(Sender: TObject);
     179var
     180  I: Integer;
     181begin
     182  //Scheduler.FMicroThreads.Clear;
     183  for I := 0 to SpinEdit1.Value - 1 do begin
     184    MainScheduler.AddMethod(MethodWorker, False);
     185  end;
    173186end;
    174187
     
    363376  ShowMessage('Exception "' + LastException.Message + '" in class "' +
    364377    LastExceptionSender.ClassName + '"')
     378end;
     379
     380procedure TMainForm.MethodWorker;
     381var
     382  I: Integer;
     383  Q: Integer;
     384begin
     385  for I := 0 to MainForm.Iterations - 1 do begin
     386    Q := 0;
     387    while Q < 100000 do Inc(Q);
     388    if MainForm.DoWriteToMemo then
     389      MainForm.Memo1.Lines.Add(IntToStr(GetCurrentMicroThread.Id) + ': ' + IntToStr(Trunc(GetCurrentMicroThread.Completion * 100)) + ' %');
     390    if MainForm.DoWaitForEvent then MainForm.Event.WaitFor(MainForm.WaitForEventDuration * OneMillisecond);
     391    if MainForm.DoSleep then MTSleep(MainForm.SleepDuration * OneMillisecond);
     392    if MainForm.RaiseException then begin
     393      MainForm.RaiseException := False;
     394      raise Exception.Create('Exception from microthread');
     395    end;
     396    if MainForm.DoCriticalSection then begin
     397      try
     398        MainForm.Lock.Acquire;
     399        MTSleep(MainForm.CriticalSectionSleepDuration * OneMillisecond);
     400      finally
     401        MainForm.Lock.Release;
     402      end;
     403    end;
     404    //WorkerSubRoutine;
     405    GetCurrentMicroThread.Completion := I / MainForm.Iterations;
     406    GetCurrentMicroThread.Yield;
     407  end;
    365408end;
    366409
Note: See TracChangeset for help on using the changeset viewer.