Ignore:
Timestamp:
Jan 10, 2025, 4:24:19 PM (7 hours ago)
Author:
chronos
Message:
  • Modified: Lowered idle CPU consumption.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/PinConnection/CommThread.pas

    r340 r414  
    1212  TReceiveDataEvent = procedure(Stream: TMemoryStream) of object;
    1313
    14   { TCommThreadReceiveThread }
    15 
    16   TCommThreadReceiveThread = class(TTermThread)
     14  { TCommThreadReceiveDataThread }
     15
     16  TCommThreadReceiveDataThread = class(TTermThread)
    1717  public
    1818    Parent: TCommThread;
     
    2424  end;
    2525
     26  { TCommThreadReceiveStatusThread }
     27
     28  TCommThreadReceiveStatusThread = class(TTermThread)
     29  public
     30    Parent: TCommThread;
     31    Stream: TBinarySerializer;
     32    procedure Execute; override;
     33    constructor Create(CreateSuspended: Boolean;
     34      const StackSize: SizeUInt = DefaultStackSize);
     35    destructor Destroy; override;
     36  end;
     37
    2638  { TCommThread }
    2739
     
    2941  private
    3042    //FOnReceiveData: TReceiveDataEvent;
    31     FReceiveThread: TCommThreadReceiveThread;
     43    FReceiveDataThread: TCommThreadReceiveDataThread;
     44    FReceiveStatusThread: TCommThreadReceiveStatusThread;
    3245    FInputBuffer: TBinarySerializer;
    3346    FInputBufferLock: TCriticalSection;
     
    91104
    92105  if AValue then begin
    93     FReceiveThread := TCommThreadReceiveThread.Create(True);
    94     FReceiveThread.FreeOnTerminate := False;
    95     FReceiveThread.Parent := Self;
    96     FReceiveThread.Name := 'CommThread';
    97     FReceiveThread.Start;
     106    FReceiveDataThread := TCommThreadReceiveDataThread.Create(True);
     107    FReceiveDataThread.FreeOnTerminate := False;
     108    FReceiveDataThread.Parent := Self;
     109    FReceiveDataThread.Name := 'CommThreadData';
     110    FReceiveDataThread.Start;
     111
     112    FReceiveStatusThread := TCommThreadReceiveStatusThread.Create(True);
     113    FReceiveStatusThread.FreeOnTerminate := False;
     114    FReceiveStatusThread.Parent := Self;
     115    FReceiveStatusThread.Name := 'CommThreadStatus';
     116    FReceiveStatusThread.Start;
    98117  end else begin
    99     FreeAndNil(FReceiveThread);
     118    FreeAndNil(FReceiveDataThread);
     119    FreeAndNil(FReceiveStatusThread);
    100120  end;
    101121  inherited;
     
    134154end;
    135155
    136 { TCommThreadReceiveThread }
    137 
    138 procedure TCommThreadReceiveThread.Execute;
    139 var
    140   TempStatus: Integer;
    141   DoSleep: Boolean;
     156{ TCommThreadReceiveDataThread }
     157
     158procedure TCommThreadReceiveDataThread.Execute;
    142159begin
    143160  with Parent do
    144161  repeat
    145     DoSleep := True;
    146162    // Check if new data arrived
    147     if FDataAvailable.WaitFor(0) = wrSignaled then begin
    148       DoSleep := False;
     163    if FDataAvailable.WaitFor(100) = wrSignaled then begin
    149164      try
    150165        FInputBufferLock.Acquire;
     
    157172      Pin.Send(Stream.List);
    158173    end;
    159 
     174  until Terminated;
     175end;
     176
     177constructor TCommThreadReceiveDataThread.Create(CreateSuspended: Boolean;
     178  const StackSize: SizeUInt);
     179begin
     180  inherited;
     181  Stream := TBinarySerializer.Create;
     182  Stream.List := TListByte.Create;
     183  Stream.OwnsList := True;
     184end;
     185
     186destructor TCommThreadReceiveDataThread.Destroy;
     187begin
     188  FreeAndNil(Stream);
     189  inherited;
     190end;
     191
     192{ TCommThreadReceiveStatusThread }
     193
     194procedure TCommThreadReceiveStatusThread.Execute;
     195var
     196  TempStatus: Integer;
     197begin
     198  with Parent do
     199  repeat
    160200    // Check if state changed
    161     if FStatusEvent.WaitFor(0) = wrSignaled then begin
    162       DoSleep := False;
     201    if FStatusEvent.WaitFor(100) = wrSignaled then begin
    163202      try
    164203        FInputBufferLock.Acquire;
     
    170209      Pin.Status := TempStatus;
    171210    end;
    172     if not Terminated and DoSleep then begin
    173       Sleep(1);
    174     end;
    175211  until Terminated;
    176212end;
    177213
    178 constructor TCommThreadReceiveThread.Create(CreateSuspended: Boolean;
     214constructor TCommThreadReceiveStatusThread.Create(CreateSuspended: Boolean;
    179215  const StackSize: SizeUInt);
    180216begin
     
    185221end;
    186222
    187 destructor TCommThreadReceiveThread.Destroy;
     223destructor TCommThreadReceiveStatusThread.Destroy;
    188224begin
    189225  FreeAndNil(Stream);
Note: See TracChangeset for help on using the changeset viewer.