coassoftwaresystems / delphi-modbus

Delphi ModbusTCP components
114 stars 61 forks source link

Non-threadsafe code in demos #19

Open BugsDigger opened 7 years ago

BugsDigger commented 7 years ago

Event OnWriteRegisters fires up in separate (not main) thread so update of string grid (sgdRegisters) should not be processed here.

procedure TfrmMain.msrPLCWriteRegisters(const Sender: TIdContext; const RegNr, Count: Integer; const Data: TModRegisterData; const RequestBuffer: TModBusRequestBuffer); var i: Integer; begin for i := 0 to (Count - 1) do SetRegisterValue(RegNr + i, Data[i]); // problem: call of VCL method in background thread end;

procedure TfrmMain.SetRegisterValue(const RegNo: Integer; const Value: Word); ... begin ... sgdRegisters.Cells[1, Index + 1] := IntToStr(Value); // VCL is not threadsafe ... end;