Board index » cppbuilder » TThread and componet property

TThread and componet property


2007-12-03 10:09:04 PM
cppbuilder105
Hello,
On my Form1 i have TPageControl with few pages.
On pages i have components like: TListView, TButtons, TEdit etc.
PageControl Visible property is set to false.
I'm creating new thread using TThread class.
In Execute() functions i have some actions and im trying to change
PageControl visible property to true and im geting that error:
"Project Project.exe raise exception class EOutOfResources with Message
'Error creating window device context.'"
Im changing property in that way:
Form1->PageControl1->Visible=true;
Of course I added Unit1.h to my thread cpp file.
I'm doing it in wrong way?
Maybe I should pass pointer to PageControl to new thread and then change it
using pointer. But what if I have few componets that properties I want
change, then i need to pass pointers to all of them?
Patryk.
 
 

Re:TThread and componet property

"Patryk B." < XXXX@XXXXX.COM >wrote in message
Quote
Hello,

On my Form1 i have TPageControl with few pages.
On pages i have components like: TListView, TButtons, TEdit etc.
PageControl Visible property is set to false.
I'm creating new thread using TThread class.
In Execute() functions i have some actions and im trying to change
PageControl visible property to true and im geting that error:

"Project Project.exe raise exception class EOutOfResources with Message
'Error creating window device context.'"

Im changing property in that way:

Form1->PageControl1->Visible=true;

VCL is not thread-safe. You have to use TThread::Synchronize() to make sure
the UI updates are done in the main thread.
--
Bruce
 

Re:TThread and componet property

On or about Mon, 3 Dec 2007 09:12:53 -0600 did " Bruce Salzman"
< XXXX@XXXXX.COM >dribble thusly:
Quote

"Patryk B." < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>Hello,
>
>On my Form1 i have TPageControl with few pages.
>On pages i have components like: TListView, TButtons, TEdit etc.
>PageControl Visible property is set to false.
>I'm creating new thread using TThread class.
>In Execute() functions i have some actions and im trying to change
>PageControl visible property to true and im geting that error:
>
>"Project Project.exe raise exception class EOutOfResources with Message
>'Error creating window device context.'"
>
>Im changing property in that way:
>
>Form1->PageControl1->Visible=true;
>

VCL is not thread-safe. You have to use TThread::Synchronize() to make sure
the UI updates are done in the main thread.
Which is clear in the docs.
What that means is you need to create a member function in the TThread
descendant that does what you want (let's say it's DoStuff), then call
it like this:
Synchronize(DoStuff);
You can't pass arguments, so you'll need to store any relevant
information in class variables.
What that does is have VCL run that bit of code on the main thread, so
if you have a lot of threads that need to call Synchronize() often,
you might want to rethink your design.
 

{smallsort}

Re:TThread and componet property

Quote
"Patryk B." < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

What that means is you need to create a member function in
the TThread descendant that does what you want (let's say
it's DoStuff), then call it like this:

Synchronize(DoStuff);
That is a simplistic view. If you examine what TThread does
and how Synchronize works, you'll find that Synchronize is
nothing more than a wrapper for the Win32 API SendMessage. How
SendMessage works is that it waits for control to return to
the caller before exection continues with the next line of
code that follows the SendMessage (Synchronize) statement.
IOW, instead of Synchronize, one might use SendMessage to send
a message to the main thread and because the current thread is
suspended waiting on the results of SendMessage ... and the processing of the message is hapening in the main thread ...
there is no GUI conflict.
The real killer here is that anyone who uses Synchronize is an
idiot. I say that because I know what Synchronize involves,
and because of that I can say with confidence that a well
crafted Win32 SendMessage can accomplish the same in a
fraction of the time.
Then there's PostMessage which can be used for a 'lazy'
GUI update (read the docs [focus on understand how and when
messages get processed] ). Admittedly, I once used Synchronize
but that was years ago. So ... save yourelf my learning curve
and expidite it ... Where ever your code says 'Synchronize',
replase that with SendMessage (to MainForm::Handle) and you should see a greate improvement in performance.
~ JD
 

Re:TThread and componet property

On or about 3 Dec 2007 23:11:35 -0700 did "JD" < XXXX@XXXXX.COM >
dribble thusly:
Quote

>"Patryk B." < XXXX@XXXXX.COM >wrote in message
>news: XXXX@XXXXX.COM ...
>
>What that means is you need to create a member function in
>the TThread descendant that does what you want (let's say
>it's DoStuff), then call it like this:
>
>Synchronize(DoStuff);

That is a simplistic view. If you examine what TThread does
[snip]
The real killer here is that anyone who uses Synchronize is an
idiot. I say that because I know what Synchronize involves,
and because of that I can say with confidence that a well
crafted Win32 SendMessage can accomplish the same in a
fraction of the time.
[snip]
Seems to me that anyone using SendMessage() is an idiot for using a
TThread descendant, instead of CreateThread().
Honestly, you seem to have no understanding of what VCL is for.
 

Re:TThread and componet property

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
If you examine what TThread does and how Synchronize works,
you'll find that Synchronize is nothing more than a wrapper for
the Win32 API SendMessage.
That was true in BCB 5 and earlier, but not in BCB 6 and later. If *you*
examine what TThread actually does now, you will see that it doesn't use
SendMessage() at all anymore. Synchronize() stores the specified method
pointer into a global TList that the main thread loops through when it is
idle. Synchronize() also uses PostMessage() to wake up the main thread if
it is an idle wait state.
Quote
The real killer here is that anyone who uses Synchronize is
an *****.
Please refrain from such statements. That is grounds for having your
messages cancelled.
Quote
I say that because I know what Synchronize involves
You know what it *used* to invoke, but clearly you haven't kept up with the
VCL's changing architecture. Synchronize() was re-written in BCB 6 in order
to support Linux.
Quote
and because of that I can say with confidence that a well
crafted Win32 SendMessage can accomplish the same in
a fraction of the time.
That is a misleading statement. Since Synchronize() used to be based on
SendMessage(), there was no speed increase in issuing your own call to
SendMessage() directly. You were accomplishing the exact same thing
manually. Also, using SendMessage() does present a potential deadlock
scenerio as well, which people did hit on from time to time.
Quote
Where ever your code says 'Synchronize', replase that with
SendMessage (to MainForm::Handle) and you should see a
greate improvement in performance.
The TWinControl::Handle property is not thread-safe. If you want a
thread-safe HWND to post/send messages to, use the TApplication::Handle
property instead, or else create your own HWND manually.
Gambit
 

Re:TThread and componet property

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

>[...] is an *****.

[...] That is grounds for having your messages cancelled.
I didn't think that that was *that* bad. Been to bpot too
often I guess.
Quote
You know what it *used* to invoke, but clearly you haven't
kept up with the VCL's changing architecture.
There has been so many times that I wished that I had
installed the VCL source when I installed BCB6 and this
is one of those times.
~ JD
 

Re:TThread and componet property

Mike Ruskai < XXXX@XXXXX.COM >wrote:
Quote

Honestly, you seem to have no understanding of what VCL is
for.
I'm sure that you could find someone to agree with you but
I doubt that you'd find anyone here.
~ JD
 

Re:TThread and componet property

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
I didn't think that that was *that* bad. Been to bpot
too often I guess.
Apparently. The off-topic newsgroup is not as strict about personal conduct
as the technical groups are.
Quote
There has been so many times that I wished that I had
installed the VCL source when I installed BCB6 and
this is one of those times.
I make it a point to always enable the source code in every version I
install. I have VCL/RTL source code going all the way back to BCB 3.
Gambit
 

Re:TThread and componet property

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote

>[...] Been to bpot too often I guess.

Apparently. The off-topic newsgroup
Full credits for getting that one! Did you google?
Quote
is not as strict about personal conduct as the technical
groups are.
Well, Rudy is the defacto prince ... what do you think?
Quote
>There has been so many times that I wished that I had
>installed the VCL source [...]

I make it a point to always enable the source code in every
version I install.
I hate myself sometimes. When I installed v6, I copied v5
source and deleted the rest (it was faster) ... never thinking
that it would make (that big of a) difference. Now I wish that
I had left v5 installed and installed v6 VCL source as well.
If you're wondering why I don't just fix it, it's because I
have the originals in a lock-box and it's a PITA to get into
it and fixing means that I have to do that twice!
I'd rather be emmbarrassed from time to time.
Quote
I have VCL/RTL source code going all the way back to BCB 3.
Doesn't that leave a gap? Did you do the same as I and learn
the hard way? What ever ... just please don't tell me that you
used VC until BCB3 <g>.
~ JD
 

Re:TThread and componet property

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
Full credits for getting that one! Did you google?
No.
Quote
When I installed v6, I copied v5 source and deleted the rest
(it was faster) ... never thinking that it would make (that big
of a) difference.
There were many changes in BCB 6, as that was the first version of BCB to
support Linux development. Much of the VCL source was shared between VCL
and CLX, just wrapped in IFDEFs where needed.
There are always smaller changes in each version. That is why I keep each
version of the source. When someone asks somthing related to a particular
version, I can see the particular quirks/features/bugs for that version.
Quote
>I have VCL/RTL source code going all the way back to BCB 3.

Doesn't that leave a gap?
No. I have source installed from every version of VCL from BCB 3 to
RADStudio 2007. Nnot all on the same machine, though. I have BCB 3-6
installed together on one machine, and BDS2006 and RADStudio2007 installed
together on another machine.
Quote
just please don't tell me that you used VC until BCB3 <g>.
I wasn't an application programmer before BCB 3. That was my first compiler
(I wrote websites and scripts before that). I had never used VC++ at all
until just a couple of weeks ago, when I needed to use it for a third-party
project I worked on.
Gambit