views:

108

answers:

1

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 ?

A: 

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.

RandomEngy