0

I'm a beginner with C# so maybe I don't really understand why I have this error.

System.InvalidOperationException: Cross-thread operation not valid: Control 'lstb_files_r' accessed from a thread other than the thread it was created on.

This is what I'm doing:

  • I drop X csv.
  • I read the first one (lstb_files_r.SelectedIndex = i;)
  • after finishing I would like to read the 2nd (i++). But I have this error...

Can someone explain to me how can I solve that please ?

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • Hint: search for english error messages, there are likely questions and answers about every single error already. If you want more *specific* help to your case, then you must include source, explaining in words is not helpful, because you are doing something wrong. See [mcve]. – Sinatr Aug 24 '20 at 09:45
  • Wpf, WinForms or something else? – Dmitry Bychenko Aug 24 '20 at 09:51

2 Answers2

0

When I asked my question I continued to look all the links and I've found something (maybe it's not the best way but I will find an other solution)

I use delegate

lstb_files_r.Invoke((MethodInvoker)delegate

I put all the code inside and it works.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
-1

You are performing the cross threaded opertaion which means you are in one thread (which is your algorithm) and want to perform something in another thread (here GUI is another thread) which is not allowed. For that you have to call GUI thread and perform the function. In C# the easiest way is to use backgraoudworker Use of backgroudworker can see here how to use background worker.

In backgroundworker use progressreport to perform the GUI operations. If you need further help I can help you.

Imran
  • 775
  • 7
  • 19
  • 1
    Let `BackroundWorker` (which is obsolete now) rest in peace; `Task`, `async`, `await` are what we should stick to – Dmitry Bychenko Aug 24 '20 at 09:53
  • @DmitryBychenko they are still in dot 5.0 version https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-5.0 If they are obsolete then they should not be in the latest package of dotnet version. Seconldy I said its easies way to use. He is new and want to learn – Imran Aug 24 '20 at 09:57