Printing in Linux with threads: Application Closes lazarus / 32-Linux ---------------------------------------------------------------------- lainz · 2022-03-30 22:40 Hi, I'm using the printers unit. In the main thread there's no problem, but using it on a separate thread causes the application to exit. Is something I'm doing wrong? Or that unit does not support threads? I'm using the printer object is already created in that unit. 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       begin         try          gbImprimiendo := True;          printer.SetPrinter(sImpresora);          printer.Title := 'POSBerry - Comanda';          printer.RawMode := True;          printer.BeginDoc;           for i:=0 to List.Count-1 do            printer.Write(List[i]);         finally          printer.EndDoc;        List.Free;          gbImprimiendo := False;        end;      end; ---------------------------------------------------------------------- lainz · 2022-04-07 20:23 Any ideas? ---------------------------------------------------------------------- MarkMLl · 2022-04-07 20:33 This is not an area in which I profess any expertise whatsoever. However in order to try to keep the ball moving and noting your courteous delay before prodding I think that there are two questions to be asked. The first is whether, when run from a shell session, the program reports any exceptions. The second is whether initiating the printing activity via the thread's Synchronize() improves reliability. MarkMLl ---------------------------------------------------------------------- winni · 2022-04-07 20:40 Quote from: lainz on March 31, 2022, 12:40:52 amI'm using the printer object is already created in that unit. Hi Lainz! Oh oh - and another thread uses the same var printer and the main thread uses the same var printer .... That's not a good idea. But why do you need a thread? Windows has a print spooler. Linux in a better way the same. And if your print direct to a printer:  Nowadays all printers have some MB buffer. So your job should in a second vanish from your app. Winni ---------------------------------------------------------------------- lainz · 2022-04-07 22:55 Thanks for the replies. On Windows seems that there's no problem. But on Linux it crashes. Maybe we need to do it synchronized anyways, because the same printer can't print two things at the same time. We're using a REST API made with lazarus for a GUI application, that receives the data to be printed. That runs in a separate thread and for that the question. I've moved the code with Synchronize() and it works. ---------------------------------------------------------------------- MarkMLl · 2022-04-08 06:40 Quote from: lainz on April 08, 2022, 12:55:19 amMaybe we need to do it synchronized anyways, because the same printer can't print two things at the same time. You're missing the point. MUCH nearer to the code you're writing, if the printing stuff provided by Lazarus isn't guaranteed to work reliably when called from a thread then you need Synchronize(). All considerations of printer sharing and document spooling are much more remote: it's what operating systems are for. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-04-08 12:39 Quote from: MarkMLl on April 08, 2022, 08:40:37 amYou're missing the point. MUCH nearer to the code you're writing, if the printing stuff provided by Lazarus isn't guaranteed to work reliably when called from a thread No, Mark, you need WaitFor.... not synchronize.. Why? Easy. Under both Linux and Windows you need to make sure the application is terminated *after* the thread has finished it job. The alternative is to not use a thread, but a separate process. ---------------------------------------------------------------------- MarkMLl · 2022-04-08 13:18 Quote from: Thaddy on April 08, 2022, 02:39:48 pmQuote from: MarkMLl on April 08, 2022, 08:40:37 amYou're missing the point. MUCH nearer to the code you're writing, if the printing stuff provided by Lazarus isn't guaranteed to work reliably when called from a thread No, Mark, you need WaitFor.... not synchronize.. Why? Easy. Under both Linux and Windows you need to make sure the application is terminated *after* the thread has finished it job. The alternative is to not use a thread, but a separate process. (Thinks, rereads thread...) OK, OP says that he's using the printers unit, while I was assuming that he was using something provided by the (Lazarus) LCL. I'm still somewhat dubious as to whether this is thread safe (on all platforms): can you say anything conclusive about that? MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-04-08 13:45 There are actually some more concerns: 1. we have a printer unit. 2. we have a printers unit. *1 is a line printer unit from the DOS era, basically writeln(, ) *2 is a more modern unit and e.g. on Windows should access the printer queue. In principle both are threadsafe, but both need to exit cleanly. Hence waitfor. The second option should be more forgiving, though, because it already delegates the job to the OS printer queue. Hence more likely to print even if the application is not terminated correctly. ---------------------------------------------------------------------- MarkMLl · 2022-04-08 14:12 I am very happy to defer to your experience in this area :-) MarkMLl