我很困惑为什么在Count()方法中使用"BeginInvoke"无法让这个测试计数器应用程序与两个(或更多)同时运行的计数文本框一起工作。
你可以通过将BeginInvoke替换为Invoke来解决该问题。但这并没有解决我的困惑。
这是我所说的示例代码:
public class CounterTextBox : TextBox
{
private int _number;
public void Start()
{
(new Action(Count)).BeginInvoke(null, null);
}
private void Count()
{
while (true)
{
if (_number++ > 10000) _number = 0;
this.Dispatcher.BeginInvoke(new Action(UpdateText), System.Windows.Threading.DispatcherPriority.Background, null);
}
}
private void UpdateText()
{
this.Text = "" + _number;
}
}
BeginInvoke
有时需要一个平衡的EndInvoke
。详情请参见:https://dev59.com/h3M_5IYBdhLWcg3wrFMt。但在这种情况下,`Control.BeginInvoke`(因此也包括`Form.BeginInvoke`)被记录为不需要`EndInvoke`的特殊情况。 - Jesse ChisholmDispatcher
没有实现ISynchronizeInvoke
。虽然这个方法的名字和那个方法相同,但在语义上明显不同。 - Servy