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??
DushiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
Costruct a thread object like this:

type
  TMyThread = class(TThread)

// Declare data about your rectangle here
    procedure execute;
    procedure DrawRect;
  end

procedure TMyThread.Execute;
begin
  While (I should keep going) do
    begin
      // Move the position of the rectange here
      synchronise(DrawRect);
    end;
end;

procedure TMyThread.DrawRect;
begin
  // Draw the rectange using the data declared in this class
end;

The synchronise call is the really important bit as this stops threads from colliding in the VCL code.

To run n rectangle around the screen do this:

for I := 1 to n do
  TMyThread.Create(False);

Cheers.

Raymond.

end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MadshiCommented:
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.
0
rwilson032697Commented:
Yes Madshi - you are right. I didn't really need to use synchronise there - just force of habit!

Cheers,

Raymond.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

DushiAuthor Commented:
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.
0
rwilson032697Commented:
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.
0
aacrgCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.