[Webinar] Streamline your web hosting managementRegister Today



Posted on 2000-01-15
Medium Priority
Last Modified: 2010-04-06
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??
Question by:Dushi
LVL 12

Accepted Solution

rwilson032697 earned 210 total points
ID: 2356034
Costruct a thread object like this:

  TMyThread = class(TThread)

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

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

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

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



LVL 20

Expert Comment

ID: 2356290
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.
LVL 12

Expert Comment

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


[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.


Author Comment

ID: 2356911
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.
LVL 12

Expert Comment

ID: 2357952
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.



Expert Comment

ID: 2359206
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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question