Reading text from multiple WPF textbox controls from a different thread

删除回忆录丶 提交于 2019-12-11 17:52:35

问题


In my WPF application I need to read text from several textboxes. Because the code is running in a different thread to the UI, I need to use Dispatcher.invoke().

At the moment I’m working with one textbox which works fine, but now I need all the texts. Do I need to write a Dispatcher.invoke for each textbox or is there a way to write a function so I pass in a textbox control reference and it returns the text?


回答1:


You could just grab the text from all the TextBox fields in the same Invoke call.

public MainWindow()
{
    InitializeComponent();

    Thread thread = new Thread(new ThreadStart(this.ThreadFunc));
    thread.Start();
}

private delegate void InvokeDelegate();
private void ThreadFunc()
{
    Dispatcher.Invoke(new InvokeDelegate(() =>
    {
        Debug.WriteLine(this.textBox1.Text + this.textBox2.Text);
    }));
}

There's no reason you would have to make multiple calls.



来源:https://stackoverflow.com/questions/2676605/reading-text-from-multiple-wpf-textbox-controls-from-a-different-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!