Reputation: 13
I'm trying to update a label value from a background thread. I understand that there are several example, but I'm still having trouble understanding why the below code throw an stack overflow error. It seems like every time setTitle() is executed it goes through the true part of the if statement.
Set title Function:
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:
delegate void setTitleDelegate(char data[]);
Thank you
Upvotes: 0
Views: 233
Reputation: 51430
Well, because of this:
d->Invoke(data);
See, here you're calling Delegate::Invoke
, which basically means that setTitle
just calls itself immediately. You need to call Control::Invoke
instead, so you need to call it on an instance of Control
, something like this:
this->lastSeen1->Invoke(d, /* any args here */);
I don't know why you're passing a char[]
here, it's better not to mix native and managed data structures too much, if you can then just use String^
instead (but even then, C++/CLI isn't really meant for UI development either...).
Upvotes: 1