Why am I getting this error:”Cross-thread operation not valid: Control lbFolders accessed from a thread other than the thread it was created on.”?

You can’t access GUI elements from a separate thread. Use a delegate to make the change.

eg.

lblStatus.Invoke((Action)(() => lblStatus.Text = counter.ToString()));

or older skool:

lblTest.Invoke((MethodInvoker)(delegate() 
{ 
  lblTest.Text = i.ToString(); 
}));

I’ve got a blog post on how to do this in all the .Net releases here.

Leave a Comment