Multithreading 对Windows窗体控件中的标签进行线程安全调用 我在微软VisualStudio中用VisualC++制作了一个小应用程序,我在一个线程中收集数据,并在Windows窗体中的标签中显示信息。我正试图遵循这篇文章/教程,了解如何调用线程安全的标签:。该示例显示了如何将文本输出到一个文本框,但我希望输出到多个标签。当我尝试时,我得到了错误:

Multithreading 对Windows窗体控件中的标签进行线程安全调用 我在微软VisualStudio中用VisualC++制作了一个小应用程序,我在一个线程中收集数据,并在Windows窗体中的标签中显示信息。我正试图遵循这篇文章/教程,了解如何调用线程安全的标签:。该示例显示了如何将文本输出到一个文本框,但我希望输出到多个标签。当我尝试时,我得到了错误:,multithreading,winforms,delegates,c++-cli,Multithreading,Winforms,Delegates,C++ Cli,错误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

错误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;
    }
}
private:
void ThreadProc()
{
当(!退出)
{
uInt8数据[100];
//获取数据的代码
SetText(数据[0].ToString(),label1);
SetText(数据[1].ToString(),label2);
SetText(数据[2].ToString(),label3);
SetText(数据[3].ToString(),label4);
SetText(数据[4].ToString(),label5);
SetText(数据[5].ToString(),label6);
...
}
}
delegate void SetTextDelegate(字符串^text);
私人:
void SetText(字符串^text,标签^Label)
{
//invokererequired比较
//向创建线程的线程ID调用线程。
//如果这些线程不同,则返回true。
如果(标签->调用所需)
{
SetTextDelegate ^d=
gcnew SetTextDelegate(this,&Form1::SetText);
这->调用(d,gcnewarray{text});
}
其他的
{
标签->文本=文本;
}
}

除了
字符串
之外,您还需要修改代理以获取
标签

delegate void SetTextDelegate(String^ text, Label^ label);
然后用两个参数调用它:

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, label });
    }
    else
    {
        label->;Text = text;
    }
}
void SetText(字符串^text,标签^Label)
{
//invokererequired比较
//向创建线程的线程ID调用线程。
//如果这些线程不同,则返回true。
如果(标签->调用所需)
{
SetTextDelegate ^d=
gcnew SetTextDelegate(this,&Form1::SetText);
这->调用(d,gcnewarray{text,label});
}
其他的
{
标签->;文本=文本;
}
}

SetText有两个参数。。。您的委托只有一个…如果我添加参数“标签^标签”,我在“行”->调用(d,gcNoxStord{tE}})上得到一个异常;“-参数计数不匹配,不管您怎么想,这不是C++。@ USS22023 26也在添加到<代码>调用()的数组中添加<代码>标签<代码>?@Andy我试过你说的话,现在效果很好。