You should only access UI elements from the thread that created them
How to: Make Thread-Safe Calls to Windows Forms Controls [
^]
Use the 'InvokeRequired' property of your listbox to see if you need to marshal back to the UI thread
e.g.
private void LoadItems()
{
if (this.listbox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
}
}
Alternatively, if you're using .Net 4.0, you might want to look at the
Task Parallel Library[
^] which allows you to use continuations and pass a Synchronization Context that automatically marshalls back to the UI
http://blogs.msdn.com/b/csharpfaq/archive/2010/06/18/parallel-programming-task-schedulers-and-synchronization-context.aspx[
^]
Edit: Just noticed the .Net 3.5 tag! I'll leave the TPL reference in my answer though, might help someone else