我正在使用线程包装器,它检查是否从主线程调用了更新 VCL(也有一些参数)的函数,然后总是在主线程的上下文中执行。
它有效,但我想让它更简单。问题是我必须在每个需要 VCL 同步的函数中重复此代码,这很容易出错。有没有办法让这个包装器更简单、更可重用?请注意,此特定包装器仅使用一个参数,但可以复制TLocalArgs
并传递任意数量的参数。
当前代码:
boost::scoped_ptr<TIdThreadComponent> WorkerThread;
...
void TForm1::SetMemoMessage(UnicodeString Msg)
{
// Check which thread called function, main thread or worker thread
if (GetCurrentThreadId() != System::MainThreadID)
{
struct TLocalArgs
{
TForm1 *Form;
UnicodeString Msg;
void __fastcall SetMemoMessage() // Same name as main function to make it easier to maintain
{
// We are in main thread now, safe to call message update directly
Form->SetMemoMessage(Msg);
}
};
// We are in worker thread, wrap into Synchronize
TLocalArgs Args = { this, Msg };
WorkerThread->Synchronize(&Args.SetMemoMessage);
return;
}
// MAIN THREAD CODE is very simple compared to wrapper above
Memo1->Text = Msg;
}