views:

592

answers:

4

i have a tcp server that, when a client connects, it makes a new thread and adds them to it but everytime i try to access information about the connection or anything about it, say even keeping a count of how many clients are connected, i get a cross-thread illegal exception or something like that.

ive read several tutorials on things called delegates, invoking and reflection but all the examples or tutorials simply confuse me as doing one invoke a certain way fails in another.

is there an elegant or simplistic way of doing this? do i need to learn how to do something else first? or am i just making things way more complex than they are? any suggestions, links or tips are most appreciated and accepted.

A: 

Using delegates and events.

That would be my best answer.

http://www.codeproject.com/KB/cs/Cross_thread_Events.aspx

Eibx
Second the CodeProject article, though I prefer the BackgroundWorker solution (also in the article). Just asynchronously invoking events is fine in many cases though.
OregonGhost
I must admit I haven't worked a lot with the BackgroundWorker class, but I'll look in to that. Thank you.
Eibx
Actually, I didn't work much with BackgroundWorker either, but I used the same concept in my last larger application and am fine with it :)
OregonGhost
A: 

There are plenty of examples online to help you: http://www.google.com/search?q=tcp+server+multi+thread

When using C#, the Concurrency and Coordination Runtime (CCR) also has a sample about implementing multithreaded tcp server. The CCR allows a much better paradigm to implement parrallel processing, it simplifies a lot of the standard multi-threading code.

Rudi
CCR sounds interesting. Is it available as a separate download, or does it only come with the Robotics Developer Studio?
OregonGhost
It comes as part of the Robotics Developer Studio, but it is a library in its own right. You can use it as you would any other .net dll. I've read that the new release (not sure it its already released) will be available as seperate download.
Rudi
+1  A: 

I suppose you go directly to the UI from your client connection thread. This is not good. Instead, consider using some variation of MVP pattern to decouple presentation logic from views. Thus, your "connection threads" will talk to some intermediary, presenters will talk to the same intermediary and just hand off some data for view to display.

As far as cross-thread operations are concerned, particulary UI thread operations, I find SynchronizationContext to be very useful in cicrumstances when you want to marshal a call from a non-UI thread to the UI thread. See this article for more in-depth discussion.

Anton Gogolev
A: 

I guess you get this cross thread exception since you are trying to update screen elements from your threaded code. If you need to do that you can get a simple solution using anonymous method.

Say that you want to add an item to a listbox called ListBoxLog. This code would do the trick from any thread:

ListBoxLog.Invoke((MethodInvoker)delegate { ListBoxLog.Items.Add("Done"); });

There's also a property to check .InvokeRequired you can check to see if invocation is nessecary. You would typically check that property in a function that could be called by both the main UI thread and any background thread.

You can also use BeginInvoke like I did with Invoke. BeginInvoke is totaly asynchronous and does not wait for the code in the delegate to finish.

sindre j