views:

1728

answers:

5

Hello,

I use the following function to show controls on my form:

class procedure TFormMain.FadeControls(ctrl:Array of TwinControl);
var element:TwinControl;
begin
  for element in ctrl do
  begin
    PrepareForAnimation(element);
    element.Visible := true;
    AnimShowControl(element,250);
  end;
end;

However,it slows down with 250 ms on each control so I'd like to put it in a thread.I read a few tutorials about theading in Delphi,but I don't understand how to create a thread with a parameter? In my case ctrl:Array of TWinControl.

I want to make a thread that does what the function above does,but I don't understand how to call it with a parameter.Threading in Delphi is harder.

Any help will be appreciated!

+4  A: 

The default threading implementation in Delphi is the TThread class. You inherited your own class and override the "execute" method, which will run in a separete thread. In other words, you can pass parameters either in the constructor or as properies of your inherited class, just make sure the parameters are passed before the thread is started ("resumed").

I am not so sure spawning threads is the right approach for what you are trying to do there, but it will sure teach you a thing or two about Delphi threading. If you have any specific questions, you know where to ask.

p.s. "Threading in Delphi is harder". Harder than what? Threading in Occam? :-)

Paul-Jan
+10  A: 

I think the problem lies in AnimShowControl as it seems to block the whole GUI for 250 ms. It should immediately return after setting up timers to do animation effects gradually (ie. not with for's/while's with Sleep calls).

Threading is not an option here, indeed it will add insult to injury in this case.

utku_karatas
+1  A: 

This may not directly answer your question, but might solve your problem-at-hand...

I had a need to run some threaded functions a while back on an application, and (frankly) didn't have the time right then to dig too far into learning about Delphi threading, or threading, period. Turned out I had a component that was included with a set of components I'd already purchased, which made what I needed to accomplish thread-wise very "accessible" and easy -- the TacThread component. I've used it a number of times to accomplish "loading" animations while running complex queries, connecting to web servers and services, etc. It might be worth a look if your looking for something quick and easy.

Jamo
+10  A: 

Since the Delphi VCL is not threadsafe, you cannot use a thread for your purpose. It's even worse: Not only is it not threadsafe, you are only allowed to call VCL code from the application's main thread.

That said, creating a thread in Delphi is as simple as declaring a descendant class of TThread, overriding its Execute method and instantiating it. That was the easy part, everything that follows is the hard part.

Sorry for being not helpful, but without knowing more about the particular control you are using, I have no idea how to solve the issue.

dummzeuch
A: 

If I'm understanding correctly what you are trying to do... animate controls in the interface without locking up the interface as each control waits 250ms for its animation to finish... you can use threads for this.

Basically, create a timer thread (based on TThread), and assign as a property of that class a callback method that references a method within your form or widget that handles the drawing. Then do your timer/looping in the execute method of the thread, and at each time increment where you want to update the visual display, call the callback from within the thread's synchronize() method, such that its executed within the apps main thread.

Basically, to launch the animation, then, you'd: - create the thread (suspended) - assign the callback and any other data needed - execute the thread - at each interval update the display via the synchronize() method - after 250ms, just let the thread finish executing and free itself.

What this will do is provide each widget a timer of its own that handles the animation without interfering with the interface while its 'waiting' in between drawing updates during the animation.

Also, you may consider some sort of thread list so that you can keep reference to each thread created, to check that they are completed before exiting the program (or you can end up with difficult to debug errors)

Instead of a callback, you can also post an event back to your main form.

Hope this helps

GrandmasterB