WPF invoke a control

47,060

When you call Invoke, you're not specifying your argument (text). When the Dispatcher tries to run your method, it doesn't have a parameter to supply, and you get an exception.

Try:

this.textboxlink.Dispatcher.Invoke(
     System.Windows.Threading.DispatcherPriority.Normal,
     new TBXTextChanger(this.WriteToTextBox), text);

If you want to read the value from a text box, one option is to use a lambda:

string textBoxValue = string.Empty;

this.textboxlink.Dispatcher.Invoke(DispatcherPriority.Normal, 
     new Action( () => { textBoxValue = this.textboxlink.Text; } ));

if (textBoxValue == string.Empty)
    Thread.Sleep(5000);
Share:
47,060
Yustme
Author by

Yustme

Updated on April 16, 2020

Comments

  • Yustme
    Yustme about 4 years

    How can I invoke a control with parameters? I've googled this up, but nowhere to find!

    invoke ui thread

    This is the error i get:

    Additional information: Parameter count mismatch.

    And this happens when i do a simple check whether the text property of a textbox control is empty or not. This works in WinForms:

    if (this.textboxlink.Text == string.Empty)
       SleepThreadThatIsntNavigating(5000);
    

    It jumps from this if the line to the catch block and shows me that message.

    This is how i try to invoke the control:

    // the delegate:
    private delegate void TBXTextChanger(string text);
    
    private void WriteToTextBox(string text)
    {
        if (this.textboxlink.Dispatcher.CheckAccess())
        {
            this.textboxlink.Text = text;
        }
        else
        {
            this.textboxlink.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new TBXTextChanger(this.WriteToTextBox));
        }
    }
    

    What am I doing wrong? And since when do i have to invoke a control when i just want to read its content?

  • Yustme
    Yustme over 13 years
    Ok, i fixed that, that was my first question. But how do i read the content of a textbox?
  • Reed Copsey
    Reed Copsey over 13 years
    Typically, you'd read it before calling the work function in the separate thread...
  • Yustme
    Yustme over 13 years
    But i have to constantly read it. not just before it. the worker thread is an endless thread, which only stops when the program closes. this app works fine in WinForm, im migrating it to WPF
  • Reed Copsey
    Reed Copsey over 13 years
    @Yustme: Technically, it could be a problem in Windows Forms, as well... What version of C#/VS are you using?
  • Yustme
    Yustme over 13 years
    okay thanks. think i need to do an extreme make over in the code. still not working.
  • Reed Copsey
    Reed Copsey over 13 years
    @Yustme: Better to just have the UI set the string, and poll on a string in a field/property. That way, you're not touching the control directly from your background thread...
  • Boern
    Boern over 9 years
    blog.somecreativity.com/2008/01/10/… might be of help as well