I should implement periodically popup form at other form. This popup form isn’t common design, so I couldn’t use standard messages. I need implement smoothly showing\hiding of this popup form.
Now I use timers for hide\show this form, but have strange problems.
If I run only Show\Hide popup form process all is OK, but when I try run other threads at base form,which are marshling to vcl thread (math painting) behaviour of popup form become strange.
How should I imlement thread-safe popup form at multi-thread app? Thanks
EDIT
Strange thing: My timers setup show show hide form with period 5s.
When process starts all is ok. Popup form is show and hide over 5s as expecting.
But then I got some cycles of popup(show popup from) withous pause.
Then again period of popup is OK(5s).
Then my timer intervals don’t work correctly.
I agree with ~4,6s period. But sometimes there is no periods between popup.
Because you left out a lot of important information (“form is behaving strange” can be interpreted in about a zillion ways, with just as many solutions), I’m going to GUESS what your problem is, and attempt giving you a solution. Please provide relevant information if I’m wrong!
About the TTimer:
The TTimer is a simple solution when you need some timing signals but it’s not supposed to be very precise. You set up the timer to “fire” at the given Interval, and Windows will send your application WM_TIMER messages with the configured periodicity. The trick here is, there will never be two WM_TIMER messages on your application’s queue at the same time.
If you were to make an clock, and used an TTimer to give you a 1 second heart-beat, your clock would be mostly on time when your computer is idle but it will run slow if your computer is busy. If the processes running the clock is busy the clock would run even slower.
About your problem:
You’re saying:
Interpretation: I assume the
thread math paintingis happening somewhere in the VCL thread, and that’s blocking your application’s message queue. This is turn is causing your application to skip WM_TIMER messages, causingthe behaviour of popup form become strange.Possible solutions:
This is obviously tough without actually knowing the problem (again, strange – how?), but I’m going to give you some ideas any way. First of all let me tell you, I don’t think you can fix your problem using a better timer. Your problem is GUI related, and the GUI is single-threaded. While you do have some background threads doing some stuff, they need to
marshall to vcl thread– no matter how precise your timer is, it can’t stop the main VCL thread from doing themarshallthing, so the timer will need to wait for themarshallto finish in order to do what needs to be done.Taking hint from
I need implement smoothly showing\hiding of this popup formI assume you need an number of steps for yoursmooth showing\hiding, and that’s what you’re using the TTimer for.If you got code like this:
(warning, this is pseudo-code, I doubt it compiles)
Replace it with something like this:
The general idea with this solution is to compute deadlines, store them in
TDateTimevariables, compare withNowfrom your TTimer.OnTimer; That way it doesn’t matter if the timer events don’t arrive at the requested intervals, you’d be mostly on time with everything else. Sure, showing up a form might not be as smooth as desired, but it’ll get the job done.