Ignore:
Timestamp:
Feb 1, 2011, 10:06:01 AM (14 years ago)
Author:
george
Message:
  • Added: Unit UThreadEx which introduce class TThreadEx with support for method CurrentThread. Unit automatic mantain runtime thread list.
  • Added: Testing of exceptions raised inside microthread in demo application.
Location:
MicroThreading
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • MicroThreading

    • Property svn:ignore
      •  

        old new  
        11lib
         2backup
  • MicroThreading/Demo

    • Property svn:ignore
      •  

        old new  
        22lib
        33Demo.exe
         4backup
  • MicroThreading/Demo/UMainForm.pas

    r159 r160  
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    99  ComCtrls, ExtCtrls, Spin, UMicroThreading, DateUtils, UPlatform,
    10   UMicroThreadList;
     10  UMicroThreadList, UThreadEx;
    1111
    1212type
     
    2929    Button3: TButton;
    3030    Button4: TButton;
     31    Button5: TButton;
    3132    ButtonAddWorkers: TButton;
    3233    ButtonClearMicroThreads: TButton;
     
    7273    procedure Button3Click(Sender: TObject);
    7374    procedure Button4Click(Sender: TObject);
     75    procedure Button5Click(Sender: TObject);
    7476    procedure ButtonSchedulerStartStopClick(Sender: TObject);
    7577    procedure Button2Click(Sender: TObject);
     
    9294  private
    9395    MicroThreadList: TMicroThreadList;
     96    LastException: Exception;
     97    LastExceptionSender: TObject;
    9498    procedure WorkerSubRoutine;
     99    procedure ShowException(Sender: TObject; E: Exception);
     100    procedure DoShowException;
    95101  public
    96102    DoWriteToMemo: Boolean;
    97103    DoSleep: Boolean;
     104    RaiseException: Boolean;
    98105    SleepDuration: Integer;
    99106    DoWaitForEvent: Boolean;
     
    121128  Event := TMicroThreadEvent.Create;
    122129  MicroThreadList := TMicroThreadList.Create(Self);
     130  UMicroThreading.ExceptionHandler := ShowException;
    123131end;
    124132
     
    150158begin
    151159  MicroThreadList.Form.Show;
     160end;
     161
     162procedure TMainForm.Button5Click(Sender: TObject);
     163begin
     164  RaiseException := True;
    152165end;
    153166
     
    335348end;
    336349
     350procedure TMainForm.ShowException(Sender: TObject; E: Exception);
     351begin
     352  LastException := E;
     353  LastExceptionSender := Sender;
     354  if MainThreadID <> ThreadID then
     355    TThread.Synchronize(TThreadEx.CurrentThread, DoShowException)
     356    else DoShowException;
     357end;
     358
     359procedure TMainForm.DoShowException;
     360begin
     361  ShowMessage('Exception "' + LastException.Message + '" in class "' +
     362    LastExceptionSender.ClassName + '"')
     363end;
     364
    337365procedure TWorker.Execute;
    338366var
     
    346374    if MainForm.DoWaitForEvent then MainForm.Event.WaitFor(MainForm.WaitForEventDuration * OneMillisecond);
    347375    if MainForm.DoSleep then MTSleep(MainForm.SleepDuration * OneMillisecond);
     376    if MainForm.RaiseException then begin
     377      MainForm.RaiseException := False;
     378      raise Exception.Create('Exception from microthread');
     379    end;
    348380    //WorkerSubRoutine;
    349381    Completion := I / MainForm.Iterations;
Note: See TracChangeset for help on using the changeset viewer.