Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
412 Beiträge
 
#49

AW: schnelle Server Client Verbindung ohne Verluste

  Alt 8. Apr 2025, 13:17
Ich rufe KEINE VCL Dinge aus einem Thread auf. Das ist mir durchaus Bewusst, dass man das NICHT darf.
Meine Log-Funktion ist entkoppelt über ein Timer (Enabled) und somit Thread-safe.

Entfernen Sie bitte das Sleep(1) und Sie werden sehen, dass die CPU Auslastung auf 6-7% geht wenn keim Empfang stattfindet. Das ist sicherlich nicht normal und daher das Sleep(1). Hat Sebastian auch so bestätigt
After running the code, i am not angry, i am amazed that is running without a exception or all kind of problem.

Any way the code somehow is running with no problem, so try the following one method and see the result
Delphi-Quellcode:
procedure TMyTCPServer.OnExecuteHandler(AContext: TIdContext);
var
  Buffer: TIdBytes;
begin
  FLastContext := AContext;
  AContext.Connection.IOHandler.ReadTimeout := IdTimeoutInfinite; // Wait indefinitely

  try
    ReadingIsActiv := True;

    AContext.Connection.IOHandler.ReadBytes(Buffer, -1, False); // blocking read -1 means read all available

    if Length(Buffer) > 0 then
    begin
      Inc(FAnzEmpfang);
      Inc(FBytesEmpfang, Length(Buffer));
      FDataQueue.Enqueue(Buffer);

      // either "TThread.Queue(nil,..", (nil for main thread) or siwtch to Synchronize
      TThread.Queue(nil, procedure
      begin
        TForm1(FForm).Log('Receive-Anzahl: ' + FAnzEmpfang.ToString);
        TForm1(FForm).Log('Receive-Bytes: ' + FBytesEmpfang.ToString);
        FAnzEmpfang := 0;
        FBytesEmpfang := 0;
      end);
    end;
  except
    on E: Exception do
    begin
      // same as above
      TThread.Queue(nil, procedure
      begin
        TForm1(FForm).Log('Error: ' + E.Message);
      end);
    end;
  end;

  ReadingIsActiv := False;
end;
Kas
  Mit Zitat antworten Zitat