How to add item to listBox from other thread?

c#, multithreading

Solution

Try this

this.Invoke((MethodInvoker)(() => OutputBox.Items.Add(engineOutput)));

Problem

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?

Original source

Related problems