Solved

Use of VCLs in threads.

Posted on 1997-04-22
6
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

615 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