Author: h.mohri
C++Builder 10.2 BCC32 can not write lambda.
So write an implementation in Invoke() with TCppInterfacedObject<>.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="synType" style="color:#3ec63e;">struct</span> T_thread_proc : TCppInterfacedObject<TProc>{ TNotifyEvent f_ev_; <span class="synType" style="color:#3ec63e;">struct</span> T_sync_proc : TCppInterfacedObject<TThreadProcedure>{ TNotifyEvent f_ev_; T_sync_proc(TNotifyEvent& ev2){ f_ev_ = ev2; } <span class="synType" style="color:#3ec63e;">virtual</span> <span class="synType" style="color:#3ec63e;">void</span> __fastcall Invoke() { <span class="synComment" style="color:#4f80e5;">//Inside Synchronize.</span> f_ev_(<span class="synConstant" style="color:#ff6666;">NULL</span>); <span class="synComment" style="color:#4f80e5;">//Execute "TNotifyEvent".</span> } }; T_thread_proc(TNotifyEvent ev1){f_ev_ = ev1; } <span class="synType" style="color:#3ec63e;">virtual</span> <span class="synType" style="color:#3ec63e;">void</span> __fastcall Invoke() { <span class="synComment" style="color:#4f80e5;">//Write thread execution code here.</span> Sleep(<span class="synConstant" style="color:#ff6666;">3000</span>); <span class="synComment" style="color:#4f80e5;">//example</span> <span class="synComment" style="color:#4f80e5;">//Finally call Synchronize.</span> TThread::Synchronize( TThread::CurrentThread,_di_TThreadProcedure(<span class="synStatement" style="color:#d88a17;">new</span> T_sync_proc(f_ev_))); } }; |
Event prepared for calling with TThread::Synchronize().
1 2 3 4 5 6 7 |
<span class="synType" style="color:#3ec63e;">void</span> __fastcall TForm1::do_thread_event(TObject *Sender) { Button1->Enabled = <span class="synConstant" style="color:#ff6666;">true</span>; ActivityIndicator1->Visible = <span class="synConstant" style="color:#ff6666;">false</span>; ActivityIndicator1->Animate = <span class="synConstant" style="color:#ff6666;">false</span>; Caption = <span class="synConstant" style="color:#ff6666;">"End CreateAnonymousThread"</span>; } |
Call TThread::CreateAnonymousThread() with a TButton event.
Place the TButton and TActivityIndicator
1 2 3 4 5 6 7 8 |
<span class="synType" style="color:#3ec63e;">void</span> __fastcall TForm1::Button1Click(TObject *Sender) { TThread::CreateAnonymousThread(_di_TProc( <span class="synStatement" style="color:#d88a17;">new</span> T_thread_proc(&do_thread_event)))->Start(); Caption = <span class="synConstant" style="color:#ff6666;">"Started CreateAnonymousThread"</span>; ActivityIndicator1->Visible = <span class="synConstant" style="color:#ff6666;">true</span>; ActivityIndicator1->Animate = <span class="synConstant" style="color:#ff6666;">true</span>; Button1->Enabled = <span class="synConstant" style="color:#ff6666;">false</span>; } |
Build execution.
When you click the Button1, until the thread is finished "ActivityIndicator1->Animate = true";
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition