简体   繁体   中英

Delphi XE Class using Parallel: how to?

I need to simulate cars moving (let's say) on a squared area. Anyone can easily imagine computations (x/y coords, track, speed, acceleration, pause...) The prior version, (Developed across Millenium Bug... 21 years ago) run into the Application.OnIdle

The current flow sounds like this:
for each aCarArray a call to TCAR.Execute is done (making computation on deltaT using GetTickCount);
for each aCarArray draw the car on then screen and updating some values
Sleep(100) to make the app looking-like responsive and again...

This approach can't take advantage of modern CPU. I'd like to move from array of TCAR's object running in the main thread to an array of Class using parallel, but I can not find any sample that shows me the a way of creating class working in parallel; computations should be done the most continuously (about one hundred cars). The goal is to develop a class that

  1. Create array of those CAR.
  2. Send a start/stop command.
  3. drawing cars on the screen (by main thread)
  4. enjoying the result (of code)

Any suggestion on the best path to start with?

Regards. Giovanni

The class you are looking can inherit from TThread. Override the Execute method to implement in a loop the processing you have to do. You can safely call Sleep to compute at the speed you want.

The most important thing in the design (Or any other threading design) is that you cannot directly access the user interface. You have either call Synchronize to execute a method in the context of the main thread or send a message from the thread class to the form or class having to do the rendering.

To draw a lot of car on screen, you may consider drawing to a TBitmap and then draw the bitmap on screen when drawing is done. This will avoid flickering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM