Multi-Threading for TBitmap, TCanvas, and TContext3D
Go Up to FireMonkey
FireMonkey supports multi-threaded use of TBitmap, TCanvas, and TContext3D. Bitmaps can be created, used, and deleted across any thread. Drawing on a canvas or 3D context is internally serialized; that is, while the code that draws can run on any thread, and several threads can draw at the same time, internally access is serialized such that only one thread can draw on a canvas or 3D context at once.
Contents
TBitmap
By using TBitmap
, you can create, destroy, and modify instances in any thread without synchronization.
For many operations, though, TBitmap
also constructs associated TCanvas
classes. Remember that some of the TCanvas
classes use thread-unsafe constructs such as TMessageManager class and TMessageManager, causing some bugs when using TBitmap
.
TCanvas
TCanvas
and all subclasses have support for multi-threaded environments, but does not support simultaneous execution of Canvas instances. This means that when a Canvas calls BeginScene
, the execution of the code inside this BeginScene...EndScene
section blocks any other thread that attempts to process drawing, including drawing on other canvases.
Write BeginScene...EndScene
blocks as short as possible to avoid blocking other threads.
For example, write:
for I := 0 to 10000 do
begin
// thread work
Canvas.BeginScene;
try
// drawing code
finally
Canvas.EndScene;
end;
end;
Instead of:
Canvas.BeginScene;
try
for I := 0 to 10000 do
begin
// thread code
// drawing code
end;
finally
Canvas.EndScene;
end;
TContext3D
TContext3D
has support for multi-threaded environments, but does not support simultaneous execution of Context3D instances. It has the same limitations as TCanvas
.