Jump to content

Global object


Roberto Nicchi

Recommended Posts

I have to share a TStringList between all sessions. Sessions needs to insert/delete items in the list.

I have defined the Tstringlist object in the uniservermodule provate section and created some public procedure to update the list. The list is created in the oncreate event and destroyed in the ondestroy event of the uniservermodule.

What's the right way to access the object in the session ?

thanks

Link to comment
Share on other sites

Hi,

TStringlist is not thread save. So would be better use a TThreadList class for such a scenario. 

https://stackoverflow.com/questions/14425871/delphi-tlist-in-multithreading

I also found this "TThreadStringList" class on the Internet (which I haven't tried):

https://www.swissdelphicenter.ch/en/showcode.php?id=2167

 

Edited by Pep
Expand reply with more Info
Link to comment
Share on other sites

15 hours ago, Roberto Nicchi said:

What's the right way to access the object in the session ?

thanks

Hello Roberto,

An other important  consideration :

Maybe the next year, Unigui will have a load-balancing process.

This mean that you will have two or many servers. This mean that your global variable value on the first server will not be known on other servers.

One Solution :

Use  DB table.

You can read this from doc :

"In future each uniGUI application can be divided into many several smaller processes to implement load-balancing and other advance feature. In this case any shared global variable in ServerModule will be visible to its owner process only not to the other processes which will server same application in a pool of processes. For this, it is recommended to keep such global variables in a database table instead of keeping them in memory."

http://www.unigui.com/doc/online_help/index.html?handling-concurrency.htm

Link to comment
Share on other sites

2 hours ago, Pep said:

Hi,

TStringlist is not thread save. So would be better use a TThreadList class for such a scenario. 

https://stackoverflow.com/questions/14425871/delphi-tlist-in-multithreading

I also found this "TThreadStringList" class on the Internet (which I haven't tried):

https://www.swissdelphicenter.ch/en/showcode.php?id=2167

 

I'll take a look to TThreadList class, thanks. Anyway what about if i use  TCriticalSection ? Is it ok ?

example:

type
  TUniServerModule = class(TUniGUIServerModule)

   procedure UniGUIServerModuleCreate(Sender: TObject);

   procedure UniGUIServerModuleDestroy(Sender: TObject);


  private
    { Private declarations }
    FTheStringList:TStringList;
    Fcs:TCriticalSection;

public

   procedure AddSomethingToTheList(avalue:string);

end;

...

procedure TUniServerModule.UniGUIServerModuleCreate(Sender: TObject);
begin
  Fcs:=TCriticalSection.Create;

  FUtenti:=TStringList.Create;

end;

procedure TUniServerModule.UniGUIServerModuleDestroy(Sender: TObject);
begin
    FTheStringList:TStringList.Free;

    Fcs.Free;

end;
 

function TUniServerModule.AddSomeThingToTheList(avalue:string);
begin
  Fcs.Acquire;
  try

    FTheStringList:TStringList.add(avalue);
  finally
    Fcs.Release;
  end;
end;

Link to comment
Share on other sites

1 hour ago, Abaksoft said:

Hello Roberto,

An other important  consideration :

Maybe the next year, Unigui will have a load-balancing process.

This mean that you will have two or many servers. This mean that your global variable value on the first server will not be known on other servers.

One Solution :

Use  DB table.

You can read this from doc :

"In future each uniGUI application can be divided into many several smaller processes to implement load-balancing and other advance feature. In this case any shared global variable in ServerModule will be visible to its owner process only not to the other processes which will server same application in a pool of processes. For this, it is recommended to keep such global variables in a database table instead of keeping them in memory."

http://www.unigui.com/doc/online_help/index.html?handling-concurrency.htm

Hello, i see. Yes, this is a thing i have to consider. Thanks.

Link to comment
Share on other sites

50 minutes ago, Roberto Nicchi said:

I'll take a look to TThreadList class, thanks. Anyway what about if i use  TCriticalSection ? Is it ok ?

example:

type
  TUniServerModule = class(TUniGUIServerModule)

   procedure UniGUIServerModuleCreate(Sender: TObject);

   procedure UniGUIServerModuleDestroy(Sender: TObject);


  private
    { Private declarations }
    FTheStringList:TStringList;
    Fcs:TCriticalSection;

public

   procedure AddSomethingToTheList(avalue:string);

end;

...

procedure TUniServerModule.UniGUIServerModuleCreate(Sender: TObject);
begin
  Fcs:=TCriticalSection.Create;

  FUtenti:=TStringList.Create;

end;

procedure TUniServerModule.UniGUIServerModuleDestroy(Sender: TObject);
begin
    FTheStringList:TStringList.Free;

    Fcs.Free;

end;
 

function TUniServerModule.AddSomeThingToTheList(avalue:string);
begin
  Fcs.Acquire;
  try

    FTheStringList:TStringList.add(avalue);
  finally
    Fcs.Release;
  end;
end;

Ok i have found in the manual that my idea to use criticalsection is correct: http://www.unigui.com/doc/online_help/index.html?handling-concurrency.htm

Anyway, for support the incoming Load Balance Server, i will remove this component and will save data into a database table.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...