C++ 使用委托从后台任务更新UI

C++ 使用委托从后台任务更新UI,c++,c++-cli,C++,C++ Cli,我正在尝试从背景线程更新标签值。我知道有几个例子,但我仍然无法理解为什么下面的代码会抛出堆栈溢出错误。似乎每次执行setTitle()时,它都会遍历if语句的真实部分 设置标题功能: void setTitle(char data[]) { String^ temp = gcnew String(data); if(this->lastSeen1->InvokeRequired) { setTi

我正在尝试从背景线程更新标签值。我知道有几个例子,但我仍然无法理解为什么下面的代码会抛出堆栈溢出错误。似乎每次执行setTitle()时,它都会遍历if语句的真实部分

设置标题功能:

    void setTitle(char data[])
    {
        String^ temp = gcnew String(data);

        if(this->lastSeen1->InvokeRequired)
        {
            setTitleDelegate^ d = gcnew setTitleDelegate(this, &setTitle);
            d->Invoke(data);
        }else
        {
            this->lastSeen1->Text = temp;
            this->lastSeen1->Visible = true;
        }

    }
代表:

delegate void setTitleDelegate(char data[]);
谢谢你

正因为如此:

d->Invoke(data);
看,这里您调用的是
Delegate::Invoke
,这基本上意味着
setTitle
只是立即调用自身。您需要调用
Control::Invoke
,因此您需要在
Control
的实例上调用它,类似这样:

this->lastSeen1->Invoke(d, /* any args here */);
我不知道为什么要在这里传递一个
char[]
,最好不要将本机数据结构和托管数据结构混合太多,如果您可以只使用
String^
(但即使如此,C++/CLI也不是真正用于UI开发的…)