Ignore:
Timestamp:
Nov 16, 2011, 7:28:20 AM (13 years ago)
Author:
george
Message:
  • Added: TExceptionLogger can be extended for handling synchronization of different thread classes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ExceptionLogger/UExceptionLogger.pas

    r265 r297  
    1010
    1111type
     12  TThreadSynchronizeEvent = procedure (AObject: TObject; Method: TThreadMethod) of object;
    1213
    1314  { TExceptionLogger }
     
    1718    FMaxCallStackDepth: Integer;
    1819    FLogFileName: string;
     20    FOnThreadSynchronize: TThreadSynchronizeEvent;
     21    procedure ThreadSynchronize(AObject: TObject; Method: TThreadMethod);
    1922    function GetAppVersion: string;
    2023    procedure SetMaxCallStackDepth(const AValue: Integer);
     
    3740    property LogFileName: string read FLogFileName write FLogFileName;
    3841    property MaxCallStackDepth: Integer read FMaxCallStackDepth write SetMaxCallStackDepth;
     42    property OnThreadSynchronize: TThreadSynchronizeEvent read FOnThreadSynchronize
     43      write FOnThreadSynchronize;
    3944  end;
    4045
     
    6368  SProcedureMethod = 'Procedure/method';
    6469  SUnit = 'Unit';
     70  SExceptionHandlerCannotBeSynchronized = 'Exception handler cannot be synchronized with main thread.';
    6571
    6672implementation
     
    8490  Application.OnException := ExceptionHandler;
    8591  Application.Flags := Application.Flags - [AppNoExceptionMessages];
     92  OnThreadSynchronize := ThreadSynchronize;
    8693end;
    8794
     
    162169  LastException := E;
    163170  ExceptionSender := Sender;
    164   if ExceptionSender is TThread then
    165     TThread.Synchronize(TThread(ExceptionSender), ShowForm)
    166     else ShowForm;
     171  if (MainThreadID <> ThreadID) then begin
     172    if Assigned(FOnThreadSynchronize) then
     173      FOnThreadSynchronize(Sender, ShowForm)
     174      else raise Exception.Create(SExceptionHandlerCannotBeSynchronized);
     175  end else ShowForm;
    167176end;
    168177
     
    209218  FMaxCallStackDepth := AValue;
    210219  StackTrace.MaxDepth := AValue;
     220end;
     221
     222procedure TExceptionLogger.ThreadSynchronize(AObject: TObject;
     223  Method: TThreadMethod);
     224begin
     225  if AObject is TThread then TThread.Synchronize(TThread(AObject), Method)
     226    else raise Exception.Create(SExceptionHandlerCannotBeSynchronized);
    211227end;
    212228
Note: See TracChangeset for help on using the changeset viewer.