Solved

Use of VCLs in threads.

Posted on 1997-04-22
6
214 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

919 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

12 Experts available now in Live!

Get 1:1 Help Now