Alright this one has me stumpted.

I have a piece of code to do TCP Listening running on a thread that was executed from the ThreadPool, using this little one liner ThreadPool.QueueUserWorkItem(ServerListener);, that will continue to run until a status flag is altered. When the thread has ended I need to clean up some resources. The only thing is I want these done on the main thread, and not within the thread spun up by the ThreadPool. This is to ensure that another piece of code doesn't run that could be counter to what I am doing with closing a connection and disposing of resources.

Is there any way I can call some sort of method on the main thread from another thread, or trigger some event (that would block calls to the class that might for instance reinitialize a variable I am trying to dispose of)?

I know I could use a BackgroundWorker for this, but would rather try to avoid those because when I read about them, the way it's worded, these seem to be intended to be used in relation with Windows Form Components, and not a simple piece of code used to send and recieve messages over TCP.

Thanks for any help.

This article has been dead for over six months. Start a new discussion instead.