我想从 SerialPort DataReceived 事件处理程序更新 UI。我发现了一个问题,因为事件处理程序隐式运行在与表单不同的线程中,所以不是简单地更新 UI ...
myLabel.Text = "Some text";
...我不得不采取以下方法:
InvokeControlAction<Label>(myLabel, lbl=> lbl.Text= "Some text");
...
public static void InvokeControlAction<t>(t cont, Action<t> action) where t : Control
{
if (cont.InvokeRequired)
{
cont.Invoke(new Action<t, Action<t>>(InvokeControlAction),
new object[] { cont, action });
}
else
{
action(cont);
}
}
到目前为止一切顺利......但是,现在我想更新 ToolStripStatusLabel - 使用相同的方法会产生“ToolStripStatusLabel 和 Forms.Control 之间没有隐式引用转换”错误。
根据我的阅读,问题源于您无法调用 ToolStripStatusLabel 的事实。
那么我该如何最好地处理这个问题呢?
注意:代表等处于我目前能力的门槛,因此将不胜感激提供解决方案的解释。
更新 1:为了澄清,我尝试创建等效于 InvokeControlAction 的 ToolStripStatusLabel,但这不起作用,因为它没有调用方法。
结果:在重新审视我的解决方案后,我已经按照 Jimmy 最初的建议将其实现为扩展方法。
我创建了一个静态ExtensionMethod类(在它自己的“ExtensionMethods”命名空间中),在 InvokeOnToolStripItem 方法中添加,添加一个“使用 ExtensionMethods;” 在我的原始类中指令并调用方法如下:
tsStatusValue.InvokeOnToolStripItem(ts => ts.Text = "ALARM signal received");