我正在用 c# 编写一个表单应用程序,我需要能够从任何线程更改富文本框的内容,我尝试使用委托和InvokeRequired,但我所做的委托仍然给我一个跨线程调用错误,并且 InvokeRequired 使表单崩溃,而不会给出错误。我需要能够从任何线程执行的功能:
public static void updateSub(int what)
{
subDisplay.subBox.Text = tb[what];
}
我尝试使用的代表:
public delegate void UpdateDelegateVoid(int what);
static public UpdateDelegateVoid uSub = new UpdateDelegateVoid(updateSub);
uSub(0);
我的 InvokeRequired 代码:
public static void updateSub(int what)
{
if (subDisplay.subBox.InvokeRequired)
{
subDisplay.subBox.Invoke(new MethodInvoker(finish));
}
else
{
subDisplay.subBox.Text = tb[what];
}
}
我不太确定为什么上面的代码不起作用。谢谢!