How to add item to listBox from other thread?
15,639
Try this
this.Invoke((MethodInvoker)(() => OutputBox.Items.Add(engineOutput)));
Related videos on Youtube
Author by
Carlj28
Updated on September 15, 2022Comments
-
Carlj28 over 1 year
I'm starting new thread:
Thread t = new Thread(UpdateListOutput); t.IsBackground = true; t.Start();
UpdateListOutput:
void UpdateListOutput() { while (true) { if (!string.IsNullOrEmpty(engineOutput)) { OutputBox.Items.Add(engineOutput); } } }
And I recive error:
Cross-thread operation not valid: Control 'OutputBox' accessed from a thread other than the thread it was created on.
How can I run this?
-
dparsonspossible duplicate of Accessing a form's control from a separate thread
-
-
kodmanyagha almost 6 yearsThis is working in mono too. I'm getting xcb_xlib_too_much_data_requested error when updating listbox from multiple threads and this is solved. Thx.
-
Yunnosch over 3 yearsIs this supposed to be an answer with a solution to the problem described in the questin at the top of the page? It reads like "I had a similar but still different problem and that I solved by .... ."
-
Sergio Quinteiro over 3 years@Yunnosch This is a similar example, like you say. The same problem (how to add a item into a ListBox from other thread), but a solution that works made on my way. The user who has the problem will have to think how to make my solution work on his code. Is only help, you know? My way works, maybe it works for him too ¿?
-
Yunnosch over 3 yearsPlease see tour and How to Answer. You seem to have misunderstood the concept of StackOverflow, which is to make a Q/A collection and not a collection of questions and discussion of similar problems. You say "The user who has the problem will have to think how to make my solution work on his code.", which means you are not discussing his problem and describing how your experience helps with that. Instead you describe your problem and your solution and expext others to transfer to the question at the top of this page. Please either delete your unrelated answer or make it anser THIS QUESTION.
-
Yunnosch over 3 yearsYou could phrase a conditional answer, like "If you problem is caused by ... then the solution is to .... because ... ." But please not as "I had a similar but different problem, maybe you can learn something from it, maybe not."