小编Jer*_*rev的帖子

Dispatcher Invoke(...)vs BeginInvoke(...)混淆

我很困惑为什么我不能让这个测试计数器应用程序使用Count()方法在我的Dispatcher上使用"BeginInvoke"的2个(或更多个)同时运行的反文本框.

您可以通过Invoke替换BeginInvoke来解决问题.但这并不能解决我的困惑.

这是我正在谈论的示例代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# multithreading invoke dispatcher begininvoke

28
推荐指数
1
解决办法
4万
查看次数

标签 统计

begininvoke ×1

c# ×1

dispatcher ×1

invoke ×1

multithreading ×1