Daniele 0 Posted December 5, 2023 (edited) Hello. I'm workin with Delphi 10.4.2 community in Windows 10/11. I've this procedure procedure TfrmMain.AddLog(NomeProc, PointProc, Datis: string; MarckEX: boolean); var ErrorLogFileName : string; ErrorFile : TextFile; markec : string; begin ErrorLogFileName := ExtractFilePath(ParamStr(0))+'error.log'; AssignFile(ErrorFile, ErrorLogFileName) ; if FileExists(ErrorLogFileName) then Append(ErrorFile) else Rewrite(ErrorFile) ; try if MarckEX then begin markec := '***;'; end else begin markec := '---;' end; ErrorData := DateTimeToStr(Now) + ';' + NomeProc + ';' + PointProc + ';' + Datis + ';' + markec; WriteLn(ErrorFile,ErrorData) ; finally CloseFile(ErrorFile) end; end; And I've this Task (simple method to work in another thread) called in a button click TASK := TTask.Create( procedure begin //do some work and then.... call AddLog.. AddLog('Test','Test','Test',false); end ); TASK.Start(); If I call AddLog into TASK do not work. I tried to call AddLog in and out to TASK := TTask.Create( procedure begin //do some work and then.... call AddLog.. TThread.Synchronize(nil, procedure begin AddLog('Test','Test','Test',false); end ); end ); TASK.Start(); but without success, nothing is ever written to the file. What am I doing wrong? Edited December 5, 2023 by Daniele Share this post Link to post
Dalija Prasnikar 1404 Posted December 5, 2023 Have you tried debugging? Besides the fact that your AddLog code is not exactly thread-safe (if you call it from multiple threads, you can get corrupted log), there is nothing there preventing it to work. And the thread-safety issue would be solved with TThread.Sysnchronize call. So if there is nothing written, your code probably never reaches AddLog call. Make sure that there is no exception raised before that call. Share this post Link to post
Daniele 0 Posted December 5, 2023 (edited) Yes, I debugged it but no exceptions are triggered. If I call outside of TTask the procedure works correctly. Edited December 5, 2023 by Daniele Share this post Link to post
Dalija Prasnikar 1404 Posted December 5, 2023 25 minutes ago, Daniele said: Yes, I debugged it but no exceptions are triggered. Saving to log file works fine to me (from the task). The only thing I noticed is that you don't have ErrorData defined within the AddLog procedure. Move it inside, as there is no reason to have broader scope. Share this post Link to post
Daniele 0 Posted December 5, 2023 (edited) Sorry but does my code work for you? Ok for ErrorData it is indeed redundant. But I'm very surprised that it works for you. I've tried and tried but I just have no way to make it work. Did you rewrite it? but oh well... Thanks anyway and best regards for the moment. Edited December 5, 2023 by Daniele Share this post Link to post
Dalija Prasnikar 1404 Posted December 5, 2023 43 minutes ago, Daniele said: Sorry but does my code work for you? Ok for ErrorData it is indeed redundant. But I'm very surprised that it works for you. I've tried and tried but I just have no way to make it work. Yes, your code works for me. so the problem must be in some other code you have. Is the AddLog procedure called at all, or just the writing to file does not work? The best thing you can do is to create empty project and test writing to log there. Share this post Link to post
Daniele 0 Posted December 5, 2023 Yes in debug I see going into the procedure and executing everything written without exceptions but it doesn't write anything to the file. I'll definitely follow your suggestion and write a clean, separate program to see what happens. Thanks very much anyway. Share this post Link to post
Daniele 0 Posted December 5, 2023 First of all, thank you again for the quick replies. For some reason in my code I had to move the addlog procedure declaration from public to private and now it works. Good work! Share this post Link to post
Dalija Prasnikar 1404 Posted December 5, 2023 36 minutes ago, Daniele said: For some reason in my code I had to move the addlog procedure declaration from public to private and now it works. This is very strange. But the whole issue has been strange from the beginning. AddLog should work both inside or outside task. The only possible explanation would be that AddLog was never called from within the task because some code before it is called triggered exception. Share this post Link to post
Daniele 0 Posted December 6, 2023 I don't know either. AddLog is specifically used to log information when the application throws exceptions. I created a demo that calls cascade procedures and each of these creates its own task and inside I generate an exception to check if it is captured and written to the log. Everything worked both by declaring the procedures involved as public and private. Evidently there is something in my application that disturbs this mechanism. In my application I use iTask to be able to read information from the internet using Indy. The fact that my application now writes logs by simply declaring procedures as private instead of public sounds strange to me too. Unfortunately I can't investigate further at the moment but thanks for your interest. Share this post Link to post