Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2007-12

Threads not working in console application

7 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I've created a while back a thread based Timer. At the time I developed the timer, I tested it in a Lazarus (LCL) GUI application and everything worked perfectly. Just tested it now and it still works fine.

Today I tried to use that Timer in a Console application and a fpGUI based application and the timer doesn't work for some reason! I'm using FPC 2.2.0 under Linux.

I attached a Console test project and the threadtimer.pas unit. Has anybody got any ideas as to why it doesn't work in a console app?

BTW:
To get the console app to continue running, I have a loop checking for a file or sleeps for 500 ms. The timer is enabled before that loop starts, so I am supposed to see output to the console. I added some debug writeln()'s to know when the time starts or stops and which events file (application loop or timer event).

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Yury Sidorov
to Graeme Geldenhuys

From: "Graeme Geldenhuys"

Show quoted text 22 lines
Hi, I've created a while back a thread based Timer. At the time I developed the timer, I tested it in a Lazarus (LCL) GUI application and everything worked perfectly. Just tested it now and it still works fine. Today I tried to use that Timer in a Console application and a fpGUI based application and the timer doesn't work for some reason! I'm using FPC 2.2.0 under Linux. I attached a Console test project and the threadtimer.pas unit. Has anybody got any ideas as to why it doesn't work in a console app? BTW: To get the console app to continue running, I have a loop checking for a file or sleeps for 500 ms. The timer is enabled before that loop starts, so I am supposed to see output to the console. I added some debug writeln()'s to know when the time starts or stops and which events file (application loop or timer event).

The thread itself works, but not Synchronize() method. You need to call CheckSynchronize inside main loop.

Yury.

Reply by email

Graeme Geldenhuys
to Yury Sidorov
Yury Sidorov wrote
The thread itself works, but not Synchronize() method. You need to call CheckSynchronize inside main loop.

Ah, thanks Yury! That also means that having the main loop fire every 500ms (it was just a test for the console app), my timer cannot fire it's events in less than 500ms (eg: timer.Interval := 200).

  while True do
  begin
    if FileExists('killme') then
      break;
    Sleep(500);
    CheckSynchronize;
    writeln('event loop');
  end;

As a test, I set the main loop's Sleep() to 10ms and the app didn't even register in 'top' for CPU usage, so I guess it's ok to set sleep() with such a small time?
Not that I intend to use the Timer interval for <500ms though.

I'll read up no CheckSyncronize to get a better understanding of this...

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys: I've created a while back a thread based Timer. At the time I developed the timer, I…
Graeme Geldenhuys wrote 10 lines
Hi, I've created a while back a thread based Timer. At the time I developed the timer, I tested it in a Lazarus (LCL) GUI application and everything worked perfectly. Just tested it now and it still works fine. Today I tried to use that Timer in a Console application and a fpGUI based application and the timer doesn't work for some reason! I'm using FPC 2.2.0 under Linux.

As far as I can see, you forget to call checksynchronize in the main thread.

BTW. There is a TThreadedTimer in the FCL, based on your code.

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
As far as I can see, you forget to call checksynchronize in the main thread.

Thanks Michael....

BTW. There is a TThreadedTimer in the FCL, based on your code.

I guess it didn't make it into FPC 2.2.0 as I searched all the sources and couldn't find any such class.

Regards,
- Graeme -

_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 11 lines
On 06/12/2007, Michael Van Canneyt wrote: > > As far as I can see, you forget to call checksynchronize in the main thread. > Thanks Michael.... > BTW. There is a TThreadedTimer in the FCL, based on your code. I guess it didn't make it into FPC 2.2.0 as I searched all the sources and couldn't find any such class.

Hm. It should be. Unit fptimer.pp.

Of course, if you used the TThreadedTimer name, it won't work. look for "TFPThreadedTimerDriver". I generalized the architecture a bit.

Michael.

Reply by email

Yury Sidorov
to Graeme Geldenhuys: Ah, thanks Yury! That also means that having the main loop fire every 500ms (it was just…

From: "Graeme Geldenhuys"

Show quoted text 25 lines
On 06/12/2007, Yury Sidorov wrote: > > The thread itself works, but not Synchronize() method. > You need to call CheckSynchronize inside main loop. > Ah, thanks Yury! That also means that having the main loop fire every 500ms (it was just a test for the console app), my timer cannot fire it's events in less than 500ms (eg: timer.Interval := 200). while True do begin if FileExists('killme') then break; Sleep(500); CheckSynchronize; writeln('event loop'); end; As a test, I set the main loop's Sleep() to 10ms and the app didn't even register in 'top' for CPU usage, so I guess it's ok to set sleep() with such a small time? Not that I intend to use the Timer interval for <500ms though.
AFAIK you can remove Sleep(500); and replace it by
CheckSynchronize(500);

Yury.

Reply by email