【发布时间】:2016-09-16 14:52:35
【问题描述】:
好的,所以这(希望)是一个非常简单的解决方法,但我正在尝试创建一个通用方法来允许外部访问标签,现在 windows 文档确实针对单个案例提供了一个示例
delegate void SetTextCallback(string text);
...some other code ...
private void SetText(string text)
{
// 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 (this.textLable.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textLable.Text = text;
}
}
但是我想创建一些更通用的东西,我可以沿着指向对象的指针的行传递一些东西,但是 Windows 窗体中的文本标签不允许这样做。理想情况下,对于这种情况,我想要一些按照这些方式做的事情(这显然不适用于形式,仅用于解释目的)
...code...
private void SetText(string text, Label* lablePointer)
{
if (this.lablePointer.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.lablePointer.Text = text;
}
}
有没有办法做到这一点?我一直在寻找,但似乎没有任何地方得到回答。
【问题讨论】:
-
为什么要使用指针??
-
因为我目前不知道更好的东西(如果有更好的方法),但这只是为了了解问题的要点。我希望我可以将它用于多个标签,以便其他线程可以访问它们并节省我为每个标签编写一百万个这样的方法。