Link to home
Start Free TrialLog in
Avatar of Dushi
Dushi

asked on

Threads

I am experimenting in Delphi 2.0. I want to get two little rectangles to move across the screen at the same time but each rectangle is a seperate object. How do I get them to move using threads??
ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Madshi
Madshi

Raymond's answer is correct - BUT neverless it doesn't make much sense (sorry, Ray)...
In Raymond's example the only thing that the threads do is to basically call Synchronize(something). Now you have to understand how Synchronize works to understand why I say that it doesn't make sense this way:
When you call Synchronize(something) the procedure "something" is NOT executed by the thread itself. What really happens is that the thread sends a message to a window that belongs to the main thread. As a result the MAIN thread executes the "something" function.
So in Raymond's example we have "n" threads which do nothing but post messages to the main thread and the main thread is doing all the work. That doesn't make sense, then you should better set up "n" timers. That would have the same effect.

This all is different if you don't need to call Synchronize. But if you use Delphi's visual VCL components, you *MUST* call Synchronize, because the VCL is still not thread safe.
So in Delphi threads make sense only if the threads do MORE then only call Synchronize and this "more" must be significantly more.
E.g. you could try to move the rects by using pure win32 APIs. They're thread safe, so you wouldn't have to use Synchronize there... On the other hand using pure win32 APIs is a pain sometimes.

Look at the win32 API "BitBlt". Does that help you? You have to give in a TCanvas.Handle into this function.

Regards, Madshi.
Yes Madshi - you are right. I didn't really need to use synchronise there - just force of habit!

Cheers,

Raymond.
Avatar of Dushi

ASKER

Thanks for the reply...but I really want to be sure that my rectangles will appear to move across a form at the same time as smoothly as possible. Also...I intend to increase the number of rectangles later on so I'm looking for a way of doing this that will be efficient so that it doesn't slow my whole program down. RWilson, if you're certain that your method will work I'll gratefully use it.
Using threads will work - so will using timers as Madshi suggests.

If you are going to eventually have a large number of rectangles then threads are probably not as good an option as using a timer to control things... (that is to say threads are probably overkill, but its what you asked for :-)

It should be fairly simple for you to implement both ways to try them out - the amount of code involved is minimal as you have seen.

Cheers,

Raymond.
If you use directly the WinApi to draw your objects you can do this in diffrent threads (see Win32 book of Microsoft). If you have to initialize some GDI objects you have to use EnterCriticalSection and LeaveCriticalSection.

Regards, aacrg.