I have a server application that receives information over a network and processes it. The server is multi-threaded and handles multiple sockets at time, and threads are created without my control through BeginInvoke and EndInvoke style methods, which are chained by corresponding callback functions.
I'm trying to create a form, in addition to the main GUI, that displays a ListBox item populated by items describing the currently connected sockets. So, what I'm basically trying to do is add an item to the ListBox using its Add() function, from the thread the appropriate callback function is running on. I'm accessing my forms controls through the Controls property - I.E:
(ListBox)c.Controls["listBox1"].Items.Add();
Naturally I don't just call the function, I've tried several ways I've found here and on the web to communicate between threads, including MethodInvoker
, using a delegate
, in combination with Invoke()
, BeginInvoke()
etc.
Nothing seems to work, I always get the same exception telling me my control was accessed from a thread other than the one it was created on.
Any thoughts?