Winforms: Access a control from another thread

0

To access a control of a form from another thread, what technique is recommended, Delegate con el invoke or class BackgroundWorker ?

Case 1 Delegate with the invoke :

delegate void SetTextCallback(string text);

private void SetText(string text){
    if( textBox1.InvokeRequired ){
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object[] { text });
    }else{
        this.textBox1.Text = text;
    }
}

Case 2 BackgroundWorker class :

private BackgroundWorker backgroundWorker1;  

private void setTextBackgroundWorkerBtn_Click(object sender, EventArgs e) {  
    this.backgroundWorker1.RunWorkerAsync();  
}  

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {  
    this.textBox1.Text = e.Result.ToString();
}  

Greetings.

    
asked by Joseba Rodríguez 04.07.2018 в 20:26
source

0 answers