Roberto Nicchi Posted August 3, 2021 Share Posted August 3, 2021 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 Quote Link to comment Share on other sites More sharing options...
Pep Posted August 4, 2021 Share Posted August 4, 2021 (edited) 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 August 4, 2021 by Pep Expand reply with more Info Quote Link to comment Share on other sites More sharing options...
Abaksoft Posted August 4, 2021 Share Posted August 4, 2021 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 Quote Link to comment Share on other sites More sharing options...
Roberto Nicchi Posted August 4, 2021 Author Share Posted August 4, 2021 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; Quote Link to comment Share on other sites More sharing options...
Roberto Nicchi Posted August 4, 2021 Author Share Posted August 4, 2021 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. Quote Link to comment Share on other sites More sharing options...
Roberto Nicchi Posted August 4, 2021 Author Share Posted August 4, 2021 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.