Re: Objects Persist Across Threads - Please Help.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Sat, 26 Jan 2008 16:43:59 -0800, pbd22 <dushkin@xxxxxxxxx> wrote:

Hi.

I have a telnet application (which many of you have already helped me
with, thank you).
I am now running into a bit of a serious design problem.

It seems that each new thread (telnet connection) is "connected" to
the pre-existing thread.
[...]

Can this be addressed conceptually or should I dump my code here (I
wasn't really sure what to post code-wise).

I don't think there's any point posting code. If you do, you would not post all of it. You would reduce the code to the bare minimum required to reproduce the problem and then post that reduced version.

That said, it sounds clear that you are using a single object instance to handle all of the data for all of the users. Obviously, this is wrong.

You should create a data structure that will contain whatever data is unique for each user. Then for each user connected, you'll create an instance of that data structure and reference it whenever you are processing a particular user.

For what it's worth, you should not be creating a new thread for each connected user. This will work okay for relatively small numbers of users, but it won't scale at all as the number of connections increases, and the code will work better with a different mechanism even for smaller users.

I prefer the asynchronous APIs, involving the methods that start with "Begin" and "End". With these methods, you pass an object reference as a "state" value to the "Begin" method. This value is then put into the IAsyncResult instance for future reference, as the IAsyncResult.AsyncState property. So, for example, if you use the callback technique (IMHO the way that is usually best), you can retrieve your object instance from the IAsyncResult passed to your callback method. Just cast it back to your own class, and you've got your per-user data for the operation (likely including the Socket reference as part of this data structure, which you'd need in the callback in order to call the "End" method, and a buffer reference, so that you can retrieve received data once an operation has completed, to name a couple of things that commonly go into a data structure like this).

Pete
.



Relevant Pages

  • Re: open() behavior under heavy disk load
    ... effects which may be created because of race conditions ... recently occured when a refence-counted data structure was supposed to ... represented while there was still an active transmission for this ... reference to the first data structure, too, because replies could come ...
    (comp.os.linux.development.system)
  • Re: Performance better with DataSet or DataReader or ...???
    ... If you were to use a DataReader to populate a DataTable ... One may argue that you'd have to do that anyway since if the data structure ... So in any case I wasn't planning to leave an open connection to ... >> public class MyClass ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: using reflection to discover a nested structure
    ... working with an instance of the object (i.e. my data structure). ... I do not want to have to reference each element of each structure ... need to find out how to reference each element of a nested array through ... FieldInfo field = t.GetField; ...
    (microsoft.public.dotnet.general)
  • Re: Dict sharing vs. duplication
    ... data structure that was treated as a first class object - instead, ... What some people would like is reference semantics. ... You could simply use [dict reference] (for ... allow you to avoid upvar in most situations. ...
    (comp.lang.tcl)
  • Re: Dict sharing vs. duplication
    ... data structure that was treated as a first class object - instead, ... Its a first class object. ... What some people would like is reference semantics. ... But here comes one of my three gripes with s. ...
    (comp.lang.tcl)