Featured post
c# - NetworkStream Pooling -
i have multi-threaded application communicates server on tcp connection. application deployed windows service.
the way has been implemeted is, there controller
creates communicator
objects, assigns port number, message count etc. properties communicator
, invokes startclient
method commence dialog server.
within startclient
method, each communicator
object creates connection server, using port number , url specified controller
. after establishing connection, internally creates thread , calls readmessages
method keeps reading server till message count met , gets closed down.
based on runtime conditions, there might need reuse communicator
object talk server again , hence, readmessages
method woudl called again.
initially, had been calling dispose()
method networkstream, streamreader , streamwriter objects when readmessages
method completed, reconnecting scenario, used throw "cannot access disposed object" error. so, commented out dispose
method call testing.
as of now, works fine, concerned that, isnt best way achieve functionlity not disposing objects ever.
i thinking in terms of object pooling, if possible have pool of stream objects reused different threads?
one way tackle can create new instance of stream objects each time communicator
connects server, think expensive operation.
can please me identify better approach handle situation here can reuse communicator
object without performance hit?
the approach based on how need read messages - if occasional n, recommend re-factor communicator object make "readmessages" operation atomic - i.e. connect server, create network stream, read messages , dispose every thing.
- Get link
- X
- Other Apps
Comments
Post a Comment