How to free a file (process) [Solved] lazarus / 21-General ---------------------------------------------------------------------- AlphaInc. · 2021-04-02 09:45 Hello everybody, I'm nearing the end of a project of mine and now i added a second language for the tool's UI. The tool does work with some batch files since I might be able to change them in the future (if i need to). Basicly every button starts a batch file which executes an external program, changes some entries in a text-file, etc. The Button captions do depend on an entry in a conf-file created by me. Therefore I have a procedure, which also runs at the start of my tool, to check my conf file and sets the Button-Captions accordingly. Now, since I've added another language, I want this conf-file to be changed during the runtime of my tool with the click of a button and after that, the tool should check the conf-file and set the Button captions. Now this is my code of how the tool reads the conf-file: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.confCheck();var   MyFile:Text;   Line1,Line2,Line3,Line4,Line5:String;begin   AssignFile(MyFile,'../Binaries/conf.ini');   Reset(MyFile);   ReadLn(MyFile,Line1);   ReadLn(MyFile,Line2);   ReadLn(MyFile,Line3);   ReadLn(MyFile,Line4);   ReadLn(MyFile,Line5); After that there are some if-conditions, which would be a little too much code to post here and I don't think it's needed for my question. The initial setting of my Button Captions work's fine but when I want to change them during the runtime (by the press of a button it starts a script which should change the conf-file and run the whole confCheck-procedure afterwards to set the Captions) it can't change the conf file since it's used by another process (which I assume is my tool?). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button5Click(Sender: TObject);begin   ShellExecute(0,nil,PChar('cmd'),PChar('/c cd  ../Binaries && start changeButtons.bat'),nil,1);   confCheck();end; The Batch scripts work, I have tested them without my tool runnign. Therefore I wanted to know, how do i free the file from my process and only load it to check the entries? ---------------------------------------------------------------------- lucamar · 2021-04-02 10:32 Did you remember to CloseFile(MyFile) somewhere after reading it in confCheck()? ---------------------------------------------------------------------- AlphaInc. · 2021-04-02 12:19 Quote from: lucamar on April 02, 2021, 12:32:04 pmDid you remember to CloseFile(MyFile) somewhere after reading it in confCheck()? Yes  I forgot about that... After adding the CloseFile(MyFile) it worked but I have stumbled upon another problem. After running the tool, I have to press the button 2 times before the Caption changes. Afterwards one button click is enough to change the Captions. Why is that so and how do I "fix" it, so that I only have to press the button one time. ---------------------------------------------------------------------- AlphaInc. · 2021-04-02 13:04 Quote from: jamie on April 02, 2021, 02:52:31 pmmyform.invalidate or myform.repaint; I would try the first one though./ Do I need to put this line before/after the Button-Click-ShellCommand or what should I do? Sorry If I don't understand right now ---------------------------------------------------------------------- AlphaInc. · 2021-04-02 13:59 Quote from: jamie on April 02, 2021, 03:50:16 pmafter you make changes to the captions. It triggers an update to the form. myform.invalidate results in an error (identifier not found). Sorry to ask dumb questions, but what do I need to enter? ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-02 14:41 Hey Alpha, Just curious about something, and I'm sorry I'm not contributing to help, but since you're reading a *.ini file why are you not using TIniFile? According to the wiki entry about executing external programs, ShellExecute is part of WinAPI. If you use TProcess or even RunCommand it will be cross-platform(both part of the FCL) and I think, at least RunCommand is easier to use than ShellExecute. Just my 2c Cheers, Gus ---------------------------------------------------------------------- AlphaInc. · 2021-04-02 14:45 Quote from: Gustavo 'Gus' Carreno on April 02, 2021, 04:41:11 pmHey Alpha, Just curious about something, and I'm sorry I'm not contributing to help, but since you're reading a *.ini file why are you not using TIniFile? According to the wiki entry about executing external programs, ShellExecute is part of WinAPI. If you use TProcess or even RunCommand it will be cross-platform(both part of the FCL) and I think, at least RunCommand is easier to use than ShellExecute. Just my 2c Cheers, Gus Well, to be honest, I only used ShellExecute and the AssignFile-method becaue it was the first solution I found  As long as it works I'm fine with that. I'm not very taletend when it comes to programming so I'm just happy that my tool runs. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-02 15:01 Hey Alpha, Quote from: AlphaInc. on April 02, 2021, 04:45:32 pmWell, to be honest, I only used ShellExecute and the AssignFile-method becaue it was the first solution I found  As long as it works I'm fine with that. I'm not very taletend when it comes to programming so I'm just happy that my tool runs. Fair enough Curiosity satisfied Will let you experts continue with the good work! And please don't beat youself down like that!! Everyone needs to learn to get talented. You don't pop up from your mom's uterus knowing how to program, right? You just need to persevere a tad more and learn some new stuff now and again, hum? I wish you tons of good luck on the project Cheers, Gus ---------------------------------------------------------------------- AlphaInc. · 2021-04-02 15:08 Quote from: Gustavo 'Gus' Carreno on April 02, 2021, 05:01:51 pmHey Alpha, Quote from: AlphaInc. on April 02, 2021, 04:45:32 pmWell, to be honest, I only used ShellExecute and the AssignFile-method becaue it was the first solution I found  As long as it works I'm fine with that. I'm not very taletend when it comes to programming so I'm just happy that my tool runs. Fair enough Curiosity satisfied Will let you experts continue with the good work! And please don't beat youself down like that!! Everyone needs to learn to get talented. You don't pop up from your mom's uterus knowing how to program, right? You just need to persevere a tad more and learn some new stuff now and again, hum? I wish you tons of good luck on the project Cheers, Gus Hey Gus, Thanks for these kind words I wish you look for your projects, too. Greetings, αlphα ---------------------------------------------------------------------- AlphaInc. · 2021-04-02 15:28 Quote from: jamie on April 02, 2021, 05:11:21 pmQuote from: AlphaInc. on April 02, 2021, 04:45:32 pmQuote from: Gustavo 'Gus' Carreno on April 02, 2021, 04:41:11 pmHey Alpha, Just curious about something, and I'm sorry I'm not contributing to help, but since you're reading a *.ini file why are you not using TIniFile? According to the wiki entry about executing external programs, ShellExecute is part of WinAPI. If you use TProcess or even RunCommand it will be cross-platform(both part of the FCL) and I think, at least RunCommand is easier to use than ShellExecute. Just my 2c Cheers, Gus Well, to be honest, I only used ShellExecute and the AssignFile-method becaue it was the first solution I found  As long as it works I'm fine with that. I'm not very taletend when it comes to programming so I'm just happy that my tool runs. I could be wrong but I think your real problem is that ShellExecute is returning before the remote app has a chance to make a change to the file. you need to either wait a little before moving to the next step or use a Wait object  For you I think it would be easier to just wait a little..  use Sleep(2000);  after the ShellExecute and before you do your updates to the controls. That value btw is for 2 secs, maybe more is need to complete the task , it depends on the PC. if that fixes the problem and you would like to have it not wait so long on faster machines, like in the future we can show you a better way. Yes, with a 2-second delay it changes to Button on first click. I can go as low as 350 milliseconds and I don't think this will bother me. But the question remains. Why do i need to wait only at the first click, without a delay? After two clicks without a delay it will work instantly. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-02 15:43 Hey all, @Jamie: If @Alpha uses RunCommand, will it implement the wait object? I know that by using TProcess you would have to do it yourself, but I've never deep dived into RunCommand's code. Cheers, Gus ---------------------------------------------------------------------- MarkMLl · 2021-04-02 19:19 Quote from: AlphaInc. on April 02, 2021, 05:28:13 pmYes, with a 2-second delay it changes to Button on first click. I can go as low as 350 milliseconds and I don't think this will bother me. But the question remains. Why do i need to wait only at the first click, without a delay? After two clicks without a delay it will work instantly. I very much suspect that this isn't related, but I've seen a problem where you first have to click on a form to get its attention and then have to (in this case double-click) to fire an event. I was never able to get to the bottom of that and emphasise that it's probably my own problem rather than something inherent in the LCL or whatever OS etc. one's running. However, I've got a bad feeling about about your problem. Relying on carefully-crafted delays is never an adequate solution since sooner or later something will change and you'll be back where you started- with the added complication of code full of attempted fixes. If you really do have to share a file between programs, you either have them operating in lockstep, or you investigate whatever file locking facilities your OS provides, or you use a proper client-server database instead. I've not seen a single program having problems re- opening a file (which is what I thought you were asking about at the start of the thread, but I confess I haven't read every intervening message), but that does emphasise the importance of making sure that files are explicitly closed in a timely fashion rather than relying on the OS to do it eventually. MarkMLl