Solved

Use of VCLs in threads.

Posted on 1997-04-22
6
217 Views
Last Modified: 2010-04-04
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?
0
Comment
Question by:djernaes
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
sperling earned 70 total points
ID: 1335609
The VCL cannot be used in threads. It was not designed to be thread safe. You'll have to use Synchronize (AThreadMethod) to execute AThreadMethod in the context of the main thread, and then you can do whatever you wan't inside this method, e.g. call Application.ProcessMessages or create/modify components.

If you need a message loop in your thread, this must be implemented from scratch. Look at the source code for Application.ProcessMessage to get a starting point. However, to idle your thread just use Sleep(0) to give up the rest of the threads time slice.

Regards,

Erik.


0
 

Author Comment

by:djernaes
ID: 1335610
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=
0
 

Author Comment

by:djernaes
ID: 1335611
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=
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:sperling
ID: 1335612
>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.
0
 
LVL 3

Expert Comment

by:sperling
ID: 1335613
>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.
0
 

Author Comment

by:djernaes
ID: 1335614
Thanks - nice and clear now :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
creating threads in delphi 1 138
DBGrid or StringGrid ? 6 105
RESTRequest Parameter 4 43
Can Live bindings change TGrid Cell Colour ? 1 5
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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