Recent

Author Topic: [SOLVED] How to create an instance of TIniPropStorage programatically  (Read 167 times)

fatmonk

  • Full Member
  • ***
  • Posts: 199
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:
Code: [Select]
  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:

Code: [Select]
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
« Last Edit: September 17, 2019, 05:40:17 pm by fatmonk »

jamie

  • Hero Member
  • *****
  • Posts: 2073
Re: How to create an instance of TIniPropStorage programatically
« Reply #1 on: September 13, 2019, 08:32:55 pm »
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.

« Last Edit: September 13, 2019, 08:40:08 pm by jamie »
Number 1 at blue screen app creations!

PascalDragon

  • Hero Member
  • *****
  • Posts: 668
  • Compiler Developer
Re: How to create an instance of TIniPropStorage programatically
« Reply #2 on: September 14, 2019, 11:08:43 am »
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.