【发布时间】:2013-08-07 21:20:39
【问题描述】:
我正在 Microsoft Visual Studio 中使用 Visual C++ 制作一个小应用程序,我在线程中收集数据并在 Windows 窗体的标签中显示信息。我正在尝试遵循这篇文章/教程,了解如何对标签线程进行安全调用:http://msdn.microsoft.com/en-us/library/ms171728(VS.90).aspx。该示例显示了如何将文本输出到一个文本框,但我想输出到多个标签。当我尝试时,我得到了错误:
错误 C3352: 'void APP::Form1::SetText(System::String ^,System::Windows::Forms::Label ^)' : 指定的函数与委托类型不匹配 'void (System: :String ^)'
这是我正在使用的一些代码:
private:
void ThreadProc()
{
while(!exit)
{
uInt8 data[100];
//code to get data
SetText(data[0].ToString(), label1);
SetText(data[1].ToString(), label2);
SetText(data[2].ToString(), label3);
SetText(data[3].ToString(), label4);
SetText(data[4].ToString(), label5);
SetText(data[5].ToString(), label6);
...
}
}
delegate void SetTextDelegate(String^ text);
private:
void SetText(String^ text, Label^ label)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (label->InvokeRequired)
{
SetTextDelegate^ d =
gcnew SetTextDelegate(this, &Form1::SetText);
this->Invoke(d, gcnew array<Object^> { text });
}
else
{
label->Text = text;
}
}
【问题讨论】:
-
您的 SetText 有两个参数...您的委托只有一个...
-
如果我添加参数“Label^ label”,我会在“this->Invoke(d, gcnew array
-
不管你怎么想,这不是 C++。
-
@user2202326 你是否也在传递给
Invoke()的数组中添加了Label? -
@Andy 我试过你说的,现在可以了。
标签: multithreading winforms delegates c++-cli