0

In my current project I have a structure like this:

Main Thread (GUI):
->Parser Thread
->Healer Thread
->Scripts Thread

the problem is that the Healer & Scripts Threads have to create childthreads with their appropiate timer, it would look like this:

->Parser Thread
->Healer Thread:
-->Healer 1
-->Healer 2
--> (...) 
->Scripts Thread:
-->Script 1
--> (...)

For doing this I have thought about coding a dynamically Timer which would be created at runtime when a new Heal/Script is added.

Now the problem/question is: maybe I have like 20 timers runing at the same time because of this, wouldn't this be a problem to my program performance (CPU consuming, etc)? Is this the best way to achieve what I'm looking for?

Thanks in advance

user2308704
  • 304
  • 2
  • 10
  • 1
    20 timers is not a problem at all. As an alternative, you can create timers with [`SetTimer`](http://msdn.microsoft.com/cs-cz/library/windows/desktop/ms644906(v=vs.85).aspx) function and have one common `WM_TIMER` message handler. But wait, where do you want to have those timers ? In threads ? – TLama May 20 '13 at 09:37
  • 1
    If you already know threads, why do you bother with timers? Put appropriate thread(s) to `Sleep` and voila. – OnTheFly May 20 '13 at 11:10
  • @user539484 so should I create a new thread (ScriptsThreadChild) to manage the childs created by ScriptsThread? and If that is correct, could I run that ScriptsThreadChild.execute() as many times as needed? like if the child threads would be running in paralallel all under the same ScriptThreadChild.pas – user2308704 May 20 '13 at 14:22
  • 2
    Why not using timer queues? They are based on OS thread pools: http://msdn.microsoft.com/en-us/library/ms686796(v=vs.85).aspx – iPath ツ May 20 '13 at 17:07

1 Answers1

1

There's no problem with having up to 20 timers active at one time in an application. Modern hardware is more than capable of handling that.

Remember also that timer messages are low priority messages and so are only synthesised when the message queue is empty. So, you need to keep the message queues of your threads serviced promptly in order for the messages to be delivered in a timely manner.

A bigger problem for you is that you cannot create TTimer instances outside the GUI/VCL thread. That's because the timer component calls AllocateHWnd which is not thread safe and can only be called from the GUI/VCL thread. So, you'll need to interact with the raw Win32 timer API directly and not use the VCL TTimer wrapper.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • oux, so can I create the SetTimer without the GUI thread? or is there a better/easier way? // @TLama: yea, since each Thread (healer/scripts) have to create child-threads with their appropiate sleeps, this is the only thing which came to my mind – user2308704 May 20 '13 at 10:07
  • 1
    Even using the raw Win32 timer API [`will still require a message loop`](http://stackoverflow.com/questions/11244848/delphi-timer-inside-thread-generates-av#comment14790920_11253254) inside the thread. For workaround take a look e.g. on [`this Q&A`](http://stackoverflow.com/q/11244848/960757) :-) – TLama May 20 '13 at 10:12
  • 1
    @user2308704 You can call `SetTimer` from any thread, but if you do so then you must service a message queue on that thread – David Heffernan May 20 '13 at 10:19
  • mmm, therefore I'll see if I can solve it as user539484 says @TLama thanks for the info also, I've been reading the code (more than twice) and couldn't really understand what you did there :( – user2308704 May 20 '13 at 14:24
  • 1
    I've created the system event (which you can imagine e.g. as a switch), which can be either signalled or not. And I'm waiting for that event to be signalled with the `WaitForSingleObject` function for a specified time (2000ms in that example). If that event is not signalled during that time, the `WaitForSingleObject` returns `WAIT_TIMEOUT` value. If it's in signalled state, it returns `WAIT_OBJECT_0`. And it becomes signalled only by calling `FinishThreadExecution` method. Otherwise it always blocks the execution of the thread on `WaitForSingleObject` function for those 2000ms. – TLama May 20 '13 at 14:43
  • So, are you looking for any more help? – David Heffernan May 20 '13 at 15:10
  • 1
    sorry, I was looking for information about what @user539484 said. Thanks a lot everyone, and thanks TLama for the explanation also =) – user2308704 May 20 '13 at 21:04
  • 2
    I spend a lot of time trying to follow what 539484 says, and it is seldom fruitful. Expect cryptic comments, and silence when things get tricky. – David Heffernan May 20 '13 at 21:04