Recent

Author Topic: Printing in Linux with threads: Application Closes  (Read 1178 times)

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Printing in Linux with threads: Application Closes
« on: March 31, 2022, 12:40:52 am »
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][+][-]
  1.       begin
  2.  
  3.         try
  4.           gbImprimiendo := True;
  5.           printer.SetPrinter(sImpresora);
  6.           printer.Title := 'POSBerry - Comanda';
  7.           printer.RawMode := True;
  8.           printer.BeginDoc;
  9.  
  10.           for i:=0 to List.Count-1 do
  11.             printer.Write(List[i]);
  12.  
  13.         finally
  14.           printer.EndDoc;
  15.           List.Free;
  16.           gbImprimiendo := False;
  17.         end;
  18.       end;

lainz

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Printing in Linux with threads: Application Closes
« Reply #1 on: April 07, 2022, 10:23:59 pm »
Any ideas?  :)

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Printing in Linux with threads: Application Closes
« Reply #2 on: April 07, 2022, 10:33:48 pm »
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Printing in Linux with threads: Application Closes
« Reply #3 on: April 07, 2022, 10:40:39 pm »
I'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

  • Hero Member
  • *****
  • Posts: 4470
    • https://lainz.github.io/
Re: Printing in Linux with threads: Application Closes
« Reply #4 on: April 08, 2022, 12:55:19 am »
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

  • Hero Member
  • *****
  • Posts: 6692
Re: Printing in Linux with threads: Application Closes
« Reply #5 on: April 08, 2022, 08:40:37 am »
Maybe 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Printing in Linux with threads: Application Closes
« Reply #6 on: April 08, 2022, 02:39:48 pm »
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
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.
« Last Edit: April 08, 2022, 02:45:12 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Printing in Linux with threads: Application Closes
« Reply #7 on: April 08, 2022, 03:18:45 pm »
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
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14377
  • Sensorship about opinions does not belong here.
Re: Printing in Linux with threads: Application Closes
« Reply #8 on: April 08, 2022, 03:45:35 pm »
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(<device>, <content>)
*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.
« Last Edit: April 08, 2022, 03:50:35 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Printing in Linux with threads: Application Closes
« Reply #9 on: April 08, 2022, 04:12:34 pm »
I am very happy to defer to your experience in this area :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018