Link to home
Start Free TrialLog in
Avatar of djernaes
djernaes

asked on

Use of VCLs in threads.

Is there any problems by using dynamicly created VCL classes in a TxxThread class? I guess that a call to the Application.ProcessMessages would not be good (another thread), but what do I do then?
ASKER CERTIFIED SOLUTION
Avatar of sperling
sperling

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of djernaes
djernaes

ASKER

When you say "the VCL" do you then mean the whole VC-Libary ? And that any kind of code in a thread must be written just for use in the thread?

Imagene that I have class called TMyClass from TComponent, then I cannot create a instance of that class in my thread and use it there?

Why not=
When you say "the VCL" do you then mean the whole VC-Libary ? And that any kind of code in a thread must be written just for use in the thread?

Imagene that I have class called TMyClass from TComponent, then I cannot create a instance of that class in my thread and use it there?

Why not=
>When you say "the VCL" do you then mean the whole VC-Libary ?

Yes. But not the RTL (System, SysUtils) or the Windows API functions (Windows, MMSystem and so on) These are thread-safe.

>And that any kind of code in a thread must be written just for
>use in the thread?

Not exactly. Mostly you can read and change simple variables in VCL objects without anything happening. But setting properties or calling methods that modify internal data or states of a class is dangerous.

E.g. to replace a list entry with another

1) Find the index in a stringlist of the first string containing 'Some string'
2) Set this entry's value to 'Another string'

If another thread intercepted this operation in between 1 and 2, and removed an entry from the same list, the first thread would modify the wrong entry.

The VCL is just not written to tackle these things. It should have been, but that's another matter...

Threads are most useful for e.g. scanning a directory tree, sending/receiving data over a network, calculation and other operations which should be performed while the user continues working.

Regards,

Erik.
>Imagene that I have class called TMyClass from TComponent, then
>I cannot create a instance of that class in my thread and use it
>there?

Descending from TComponent, probably you could. If your implementation is thread-safe, and the component doesn't write to any shared resources or resources used by another thread.

Regards,

Erik.
Thanks - nice and clear now :-)