Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Get Textbox value in backgroundworker dowork event

In my windows forms application I have a textbox and backgroundworker component. In dowork event of the backgroundworker I am trying to access value of the textbox. How can i do that? I'm getting following exception in dowork event handler code when I try to access value of the textbox:

Cross-thread operation not valid: Control 'txtFolderName' accessed from a thread other than the thread it was created on`
like image 974
user1941944 Avatar asked Apr 02 '13 09:04

user1941944


1 Answers

You can only access textbox / form controls in GUI thread, you can do so like that.

if(txtFolderName.InvokeRequired)
{
    txtFolderName.Invoke(new MethodInvoker(delegate { name = txtFolderName.text; }));
}
like image 66
Adil Avatar answered Oct 15 '22 06:10

Adil