Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access values on the UI thread?

I have no issues invoking actions on UI controls through BeginInvoke and such, however, that's a void method. I need to get a value from a textbox for use in a different thread.

How can I accomplish this?

like image 612
Corey Avatar asked Jan 29 '26 08:01

Corey


2 Answers

The simplest solution is to capture a local variable in a closure.

String text;
textBox.Invoke(() => text = textBox.Text);

The compiler will generate some code that is much like chibacity's solution - the local variable becomes a field of a compiler-generated class.

UPDATE

This does not work - the lambda expression is not assignable to Delegate. This problem can be solved using an extension method.

internal void ExecuteOnOwningThread(this Control control, Action action)
{
    if (control.InvokeRequired)
    {
        control.Invoke(action);
    }
    else
    {
        action();
    }
}

The usage is then as follows.

String text;
textBox.ExecuteOnOwningThread(() => text = textBox.Text);

It is possible to stuff multiple statements into the lambda expression.

textBox.ExecuteOnOwningThread(() =>
{
    DoStuff();
    text = textBox.Text
    DoOtherStuff();
});

But as chibacity already mentioned in a comment it may be better to explicitly write a method. Beyond a certain point using lambda expressions will adversely affect the readability of the code. And using lambda expressions is of course very prone to introducing repeated code.

like image 176
Daniel Brückner Avatar answered Jan 30 '26 22:01

Daniel Brückner


The Control.Invoke() method returns the value of the invoked method. Which makes it easy:

        string value = (string)this.Invoke(new Func<string>(() => textBox1.Text));

Or a bit more explicit:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
        var dlg = new Func<string>(GetText);
        string value = (string)this.Invoke(dlg);
    }
    private string GetText() {
        return textBox1.Text;
    }
like image 43
Hans Passant Avatar answered Jan 30 '26 21:01

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!