线程仅在调试模式下接收数据

时间:2016-01-11 11:09:46

标签: multithreading delphi indy indy10

这是IdTCPClient中传入的Delphi连接的阅读主题。

unit UnitWorkerThread;

interface

uses
    System.Classes,
    IdTCPClient;

type
    TDataEvent = procedure(const Data: Byte) of object;

    TReadingThread = class(TThread)
    private
        FClient: TIdTCPClient;
        FData  : Byte;
        FOnData: TDataEvent;
        procedure DataReceived;
    protected
        procedure Execute; override;
    public
        constructor Create(AClient: TIdTCPClient); reintroduce;
        property OnData: TDataEvent read FOnData write FOnData;
    end;

implementation

constructor TReadingThread.Create(AClient: TIdTCPClient);
begin
    inherited Create(True);
    FClient := AClient;
end;

procedure TReadingThread.Execute;
begin
    while not Terminated do
    begin
        FData := FClient.IOHandler.ReadByte;
        if Assigned(FOnData) then
            Synchronize(DataReceived);
    end;
end;

procedure TReadingThread.DataReceived;
begin
    if Assigned(FOnData) then
        FOnData(FData);
end;

end.

这就是我在Form上创建和处理它的方式。

procedure TFormLogin.IdTCPClient1Connected(Sender: TObject);
var

    Thread  : TReadingThread;

begin

    try
        Thread        := TReadingThread.Create(IdTCPClient1);
        Thread.OnData := DataReceived;
        Thread.Start;

    except
    end;

end;

procedure TFormLogin.DataReceived(const Data: Byte);
begin
        FormHome.ModbusClient1.PutRxData(Data);
end;

逐步调试时,我无法理解为什么接收数据

可能导致什么呢?

0 个答案:

没有答案