gpt4 book ai didi

c++ - 使用委托(delegate)从后台任务更新 UI

转载 作者:行者123 更新时间:2023-11-28 04:55:41 26 4
gpt4 key购买 nike

我正在尝试从后台线程更新标签值。我知道有几个例子,但我仍然无法理解为什么下面的代码会抛出堆栈溢出错误。似乎每次执行 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[],最好不要过多地混合 native 和托管数据结构,如果你可以只使用 String^ 而不是(但即便如此,C++/CLI 也不是真正适合 UI 开发的……)。

关于c++ - 使用委托(delegate)从后台任务更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47168698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com