Changeset 271 for Common


Ignore:
Timestamp:
Aug 30, 2011, 3:47:29 PM (13 years ago)
Author:
george
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/UThreading.pas

    r267 r271  
    8989  TTermThread = class(TListedThread)
    9090  private
     91    FOnFinished: TNotifyEvent;
    9192  public
    9293    State: TTermThreadState;
     
    9495    Method: TMethodCall;
    9596    procedure Execute; override;
     97    property OnFinished: TNotifyEvent read FOnFinished write FOnFinished;
    9698  end;
    9799
     
    109111
    110112procedure RunInThread(Method: TMethodCall);
     113procedure RunInThreadAsync(Method: TMethodCall; Callback: TNotifyEvent = nil);
    111114procedure Synchronize(Method: TMethodCall);
    112115
     
    124127    Thread := TTermThread.Create(True);
    125128    Thread.FreeOnTerminate := False;
     129    Thread.Method := Method;
    126130    Thread.Resume;
    127     Thread.Method := Method;
    128131    while (Thread.State = ttsRunning) or (Thread.State = ttsReady) do begin
    129132      if MainThreadID = ThreadID then Application.ProcessMessages;
     
    137140end;
    138141
     142procedure RunInThreadAsync(Method: TMethodCall; Callback: TNotifyEvent = nil);
     143var
     144  Thread: TTermThread;
     145begin
     146  try
     147    Thread := TTermThread.Create(True);
     148    Thread.FreeOnTerminate := True;
     149    Thread.Method := Method;
     150    Thread.OnFinished := CallBack;
     151    Thread.Resume;
     152    //if Thread.State = ttsExceptionOccured then
     153    //  raise Exception.Create(Thread.ExceptionMessage);
     154  finally
     155  end;
     156end;
     157
    139158procedure Synchronize(Method: TMethodCall);
    140159var
     
    151170end;
    152171
    153 { TVirtualThread }
    154 
    155172
    156173{ TThreadList }
     
    186203                                                                                                                                                                                                                                                                                                                                    end;
    187204
    188 { TVirtualThread }
    189 
    190 
    191205{ TListedThread }
    192206
     
    324338    Method;
    325339    State := ttsFinished;
     340    if Assigned(FOnFinished) then
     341      FOnFinished(Self);
    326342  except
    327     on E: Exception do
    328       if Assigned(OnException) then begin
     343    on E: Exception do begin
     344      ExceptionMessage := E.Message;
     345      State := ttsExceptionOccured;
     346      if Assigned(OnException) then
    329347        OnException(FThread, E);
    330         ExceptionMessage := E.Message;
    331         State := ttsExceptionOccured;
    332       end;
     348    end;
    333349  end;
    334350end;
Note: See TracChangeset for help on using the changeset viewer.