Modify visual element by another thread

6

I want to define a Thread in a RichTextBox but I get an error saying

  

Can not make calls from another thread than the same TextBox

I've heard that it's possible to use the Control.Invoke option, so I was very much in doubt at that point.

In short, I want to develop an APP that does not start the CMD with the window, follow commands and can send commands to the CMD, I already have the code ready but at the time I configure RedirectStandardOutput=true ends up only when the process closes it shows the log.

So I'd like to implement a Thread on top of the textbox to with no need to close the process and show the "log".

    
asked by anonymous 18.10.2015 / 00:46

1 answer

6

As you have not shown the code of what you are trying to do, I will give you a more general answer.

You can do this

this.Invoke(new MethodInvoker(() => textBox.Text = "NovoTexto"));

I would do a function to change the value of TextBox , where in the function itself it would check if it is necessary to use Invoke or not. This is more a matter of taste. If it interests you, it would stay that way.

public void SetText(TextBox txtbox, string texto) 
{
    if (txtbox.InvokeRequired)
        txtbox.Invoke(new MethodInvoker(() => txtbox.Text = texto));
    else
        txtbox.Text = texto;        
}

Use

SetText(textBox, "Novo Texto");
    
18.10.2015 / 01:53