Z
zw84611
Unregistered / Unconfirmed
GUEST, unregistred user!
'does not allow drawing' 主要是因为VCL是非线程安全的,在线程中使用VCL控件(尤其是可视
控件,甚至包括ShowMessage这样的过程)应使用Synchronize(),否则会有隐患。
详细的介绍可以看Delphi中关于Synchronize的帮助:
When you use objects from the VCL object hierarchy, their properties and methods
are not guaranteed to be thread-safe. That is, accessing properties or executing
methods may perform some actions that use memory which is not protected from the
actions of other threads. Because of this, a main VCL thread is set aside for
access of VCL objects. This is the thread that handles all Windows messages
received by components in your application.
If all objects access their properties and execute their methods within this
single thread, you need not worry about your objects interfering with each
other. To use the main VCL thread, create a separate routine that performs the
required actions. Call this separate routine from within your thread抯
Synchronize method. For example:
procedure TMyThread.PushTheButton;
begin
Button1.Click;
end;
procedure TMyThread.Execute;
begin
...
Synchronize(PushTheButton);
...
end;
Synchronize waits for the main VCL thread to enter the message loop and then
executes the passed method.
Note: Because Synchronize uses the message loop, it does not work in
console applications. You must use other mechanisms, such as critical sections,
to protect access to VCL objects in console applications.
You do not always need to use the main VCL thread. Some objects are thread-aware
. Omitting the use of the Synchronize method when you know an object抯 methods
are thread-safe will improve performance because you don抰 need to wait for the
VCL thread to enter its message loop. You do not need to use the Synchronize
method in the following situations:
Data access components are thread-safe as long as each thread has its own database
session component. The one exception to this is when you are using Access
drivers. Access drivers are built using the Microsoft ADO library, which is not
thread-safe.
When using data access components, you must still wrap all calls that involve
data-aware controls in the Synchronize method. Thus, for example, you need to
synchronize calls that link a data control to a dataset by setting the DataSet
property of the data source object, but you don抰 need to synchronize to access
the data in a field of the dataset.
For more information about using database sessions with threads, see Managing
multiple sessions.
Graphics objects are thread-safe. You do not need to use the main VCL thread to
access TFont, TPen, TBrush, TBitmap, TMetafile, or TIcon. Canvas objects can be
used outside the Synchronize method by locking them.
While list objects are not thread-safe, you can use a thread-safe
version, TThreadList, instead of TList.
对于DataReceived的问题,我也搞不太清楚。
控件,甚至包括ShowMessage这样的过程)应使用Synchronize(),否则会有隐患。
详细的介绍可以看Delphi中关于Synchronize的帮助:
When you use objects from the VCL object hierarchy, their properties and methods
are not guaranteed to be thread-safe. That is, accessing properties or executing
methods may perform some actions that use memory which is not protected from the
actions of other threads. Because of this, a main VCL thread is set aside for
access of VCL objects. This is the thread that handles all Windows messages
received by components in your application.
If all objects access their properties and execute their methods within this
single thread, you need not worry about your objects interfering with each
other. To use the main VCL thread, create a separate routine that performs the
required actions. Call this separate routine from within your thread抯
Synchronize method. For example:
procedure TMyThread.PushTheButton;
begin
Button1.Click;
end;
procedure TMyThread.Execute;
begin
...
Synchronize(PushTheButton);
...
end;
Synchronize waits for the main VCL thread to enter the message loop and then
executes the passed method.
Note: Because Synchronize uses the message loop, it does not work in
console applications. You must use other mechanisms, such as critical sections,
to protect access to VCL objects in console applications.
You do not always need to use the main VCL thread. Some objects are thread-aware
. Omitting the use of the Synchronize method when you know an object抯 methods
are thread-safe will improve performance because you don抰 need to wait for the
VCL thread to enter its message loop. You do not need to use the Synchronize
method in the following situations:
Data access components are thread-safe as long as each thread has its own database
session component. The one exception to this is when you are using Access
drivers. Access drivers are built using the Microsoft ADO library, which is not
thread-safe.
When using data access components, you must still wrap all calls that involve
data-aware controls in the Synchronize method. Thus, for example, you need to
synchronize calls that link a data control to a dataset by setting the DataSet
property of the data source object, but you don抰 need to synchronize to access
the data in a field of the dataset.
For more information about using database sessions with threads, see Managing
multiple sessions.
Graphics objects are thread-safe. You do not need to use the main VCL thread to
access TFont, TPen, TBrush, TBitmap, TMetafile, or TIcon. Canvas objects can be
used outside the Synchronize method by locking them.
While list objects are not thread-safe, you can use a thread-safe
version, TThreadList, instead of TList.
对于DataReceived的问题,我也搞不太清楚。