sevenOfNine
sevenOfNine

Reputation: 1554

TThread::Synchronize() > when reading Form component properties?

My Environment:

I have a question about TThread:Synchronize().

Usually, I use Synchronize() when I update Form component (e.g. Text->Caption) from the TThread routine().

__fastcall TThreadSample::Execute()
{
    Synchronize(&updateFormText);
}

where the updateFormText() is a function to update Form Text->Caption.

On the other hand, when I read caption from the Form text, I used followings without Synchronize().

__fastcall TThreadSample::DoRead()
{
    String acap = CFormXXX::GetTextCaption();
}

void __fastcall CFormXXX::GetTextCaption()
{
    return FormXXX->TextXXX->Text;
}

Question: Do I have to use Synchronize() also when I read Form component properties from a TThread routine?

Upvotes: 0

Views: 137

Answers (1)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

Yes.

Properties are in fact syntactic sugar for function calls. Reading one also means a function is called (well, most of the time[1]).

In other words, if, in your code, you do:

x = MyVCLObj->SomeProperty;

the C++Builder compiler in fact generates a call to the (usually private) getter function for the property:

x = MyVCLObj->GetSomeProperty();

That runs in the context of the main thread, so it must be accessed using Synchronize().


[1] I know that this is not true for all properties, and you may well be accessing the member field (e.g. FSomeProperty) directly, but do you really want to check the docs each time? And this may change in a future version of the class too. So you should generally treat a property access like a function call.

Upvotes: 1

Related Questions