Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

Threads and system clock

I had used Inter's solution for setting a timer as my application requires the timer to work from a DLL and I could not use the TTimer component. Inter solution can be seen here:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_10090518.html

The solution works great, but for some reason, it stops ticking when the main application is doing some hard work.

I had called the DLL from a separate thread but that did not solve the problem.

Can anyone suggest a way to hook up to a timer that will not freeze when the application is hard at work?

Thanks,
Asaf
0
paladin27
Asked:
paladin27
  • 4
  • 4
  • 2
  • +1
1 Solution
 
paladin27Author Commented:
Dear Pavel,

Thanks for your quick reply and the useful links.

I am afraid that I was not clear enough. I need to use the timer for an action that needs to take place 100-250 times a second.

My problem is that when my program is hard at work, I stop getting timer ticks for more than 2 seconds, which is very bad for me.

Thanks,
Asaf
0
 
pgnatyukCommented:
I answered - I think you need to use QueryPerformanceCounter. Please read my previous comment. Two last links contain a Delphi code and an explanation.
 
 
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
paladin27Author Commented:
Dear Pavel,

Thanks again for your reply. If I read correctly, the links you had sent are about timing Delphi code, i.e. measuring the time it takes to execute a block of code. Am I right?

What I am after is a solution that would allow me get timer ticks as an event. My code needs to execute an action 100-250 times a second. It currently depends on inter's solution and stops working for almost 3 seconds when the program is busy.

I am after a robust way of getting my tick events every 4 ms, allowing temporarily faults of up to 50ms, but no longer.

Thanks,
Asaf
0
 
LMiller7Commented:
The issue is probably that when your app is busy it isn't able to handle handle timer messages. When this occurs Windows will stop sending them until the backlog is cleared. You may have to rework your code. I don't think that receiving a timer message every 4ms is realistic.
0
 
pgnatyukCommented:
Yes, you understand correctly.

I hope I understand. The code you are using right now is the Windows timer set for 4 msec.
I'd say this is already a mistake. Firstly, because you cannot expect such quality from the windows timer. Secondly, if the operation you perform on the timer event takes longer than 4 msec (for example, you redraw the window)... you are in a logical problem. One more point, the timer message has almost the lowest priority.

In such case, I'd say, you need to use the idle time of the window message loop (if you know how to get there with Delphi). If it is about graphics - you need to use the double buffering. Many OpenGL applications uses this way. I think, this the right way that every one here will recommend.

You may create a thread with WaitForMultipleObjects inside that will post a message to your window (or call invalidate) - it's, actually, the same as you use now, but, I trust more to WaitForMultipleObjects. You understand that WaitMultipleObjects in this case is just Sleep(4).
0
 
paladin27Author Commented:
I'd like to thank you, again, for having replied, but I feel that my case is not clear enough and, as I said, it of high importance to me. I now realize that perhaps it is not as simple as I thought so I will try and raise the point value to 250.

Pavel - My application is not multimedia, I am aware that 4ms may be a stretch, but as I said, even if, at times, the tick will occur after 50ms, it is still fine.

LMiller - I was hoping that as the part of my app that is waiting for the clock is working as a seperate thread, that would make it OK.

If anyone can tell me that it is not the case, and placing my code in a separate thread does not solve the problem, I'd be grateful.
0
 
pgnatyukCommented:
If you are talking about 50msec time interval, you can use your approach. You need to use GetTickCount, for example, and count how long is your timer handling procedure. If it takes longer than 50msec, optimize the code.
0
 
LMiller7Commented:
Windows is not a real time operating system. No thread has any assurances of how much CPU time it will receive or how often it will have control of a CPU core. It seems clear that the thread handling messages is overburdened and cannot keep up. I think you must solve the problem with an approach that does not involve such a high rate of timer messages.
0
 
Geert GruwezOracle dbaCommented:
if your app can actually raise a event every 4 msecs,
which i agree, shouldn't be a problem

but what does the event trigger ?
slow code ? fast code ? starting a thread ?
just running some code in the vcl foreground ?
what does the triggered code actually do ?
0
 
paladin27Author Commented:
Sorry for the delay in response.
My application is connected to a hand scanner. The scanner sends information continuesly and I need to read a 128 byte worth of data every 4 ms or 256 byte every 8 ms and so on.

After havin giving up in on geiving up on getting the job done in a single thread with other tasks that takes away processor time I had simply placed the task that needs to work with the hardware in a seperate process setting its prioity to high (setting it to realtime caused the system to freeze).

Now, after getting comments saying that a process should not have 'High' Priority I am looking at creating a multithreaded application that runs the thread that works with the hardware in 'High' priority.

Setting High priority to a thread is, for some reason, safer.

Thanks again to everyone.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now