Lazarus
Programming => General => Topic started by: fatmonk on September 13, 2019, 07:31:14 pm
-
I want to create and manage a heartbeat file in a thread - I was doing this on the main UI thread but it was causing the app to be non-responsive so I'm moving it out into a thread of its own.
I am using a TIniPropStorage as the container of the heartbeat file as it gives me easy access to a couple of values stored in the file.
I am declaring the object as:
heartbeatFile: TIniPropStorage;
but when I try to assign the IniFileName I get an External: SIGNSEGV crash at run time. The assembler view shows that it is the fpc_ansistr_assign that is the problem.
Do I need to create the instance of the object before I can use it (because it is not auto created as its not on the main thread?) ?
Or is something else wrong here?
This is the relevant code:
uses
Classes, SysUtils, FileUtil, IniPropStorage;
type
{THeartBeatThread}
THeartBeatThread = class(TThread)
heartbeatFile: TIniPropStorage;
private
fStatusText: string;
procedure ShowStatus;
procedure heartbeat();
protected
procedure Execute; override;
public
var
heartbeatFileName: AnsiString;
heartbeatPeriod: Integer;
beating: Boolean;
constructor Create(CreateSuspended: boolean);
end;
implementation
{ THeartBeatThread }
uses MainUnit;
procedure THeartBeatThread.ShowStatus;
begin
MainForm.addToLog(fStatusText);
end;
procedure THeartBeatThread.Execute;
begin
heartbeatFile.IniFileName:=heartbeatFileName;
end;
{etc etc}
I've tried adding heartbeatFile := TIniPropStorage.Create(); before trying to assign the filename, but I do not know what TComponent I should be using as the argument to the Create() method.
Thanks,
FM
-
You need to create it before you interact with it..
MyIniThing := TiniPropStorage.Create(Nil); you don't need to specify an owner but you do need to free it..
If you do specify an owner, the owner will free it for you if you have not readly..
Freeing it closes the file.
You also need to specify the file name
MyIniThing.FileName := …..
etc...
I wanted to mention that what you are doing isn't thread safe.
you need to make a few Sync Procedures your thread can call.
any values you need to pass to the base thread you can do so using global variables etc.
-
I wanted to mention that what you are doing isn't thread safe.
you need to make a few Sync Procedures your thread can call.
any values you need to pass to the base thread you can do so using global variables etc.
Don't use global variables for synchronization purposes. If the thread is created multiple times that won't be thread safe either. Use private fields of your class for that.