Solved

Use of VCLs in threads.

Posted on 1997-04-22
6
213 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:sperling
Comment Utility
>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
Comment Utility
>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
Comment Utility
Thanks - nice and clear now :-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now