none
sockets in spawn threads RRS feed

  • Question

  • hi everybody. i have a program (c#) that has a socket listener that accpets incoming tcp connections. and when it receives one, it will redirect it to a new thread so that the port can be freed up and will be ready to accept another tcp connection. so basically this program will spawn many threads depending on the  number of connections made. my problem is if connections are not properly closed it will leave behind the thread open even though it is unused anymore. So what i want to do is create a function that will scan through all the threads that are spawned and check if some of them has no more activity or idle and kill the thread. any ideas on how can i attain this? i mean is there  method or property that check if a certain thread is idle or has no more activity? i hope somebody here can help me..
    Thursday, October 1, 2009 6:26 AM

Answers

  • Use TcpListener.BeginAcceptSocket().  The callback will run on a background thread when a client connects.

    Hans Passant.
    • Proposed as answer by Harry Zhu Thursday, October 8, 2009 2:29 AM
    • Marked as answer by liurong luo Thursday, October 8, 2009 11:04 AM
    Thursday, October 1, 2009 11:32 AM
    Moderator

All replies

  • Hi,

    AFAIK, there is nothing like this. You would need to implement kind of wrapper over each thread and follow HeartBeat pattern (each thread if it is executing updates appropriate value with last execution time).
    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Thursday, October 1, 2009 10:47 AM
  • Use TcpListener.BeginAcceptSocket().  The callback will run on a background thread when a client connects.

    Hans Passant.
    • Proposed as answer by Harry Zhu Thursday, October 8, 2009 2:29 AM
    • Marked as answer by liurong luo Thursday, October 8, 2009 11:04 AM
    Thursday, October 1, 2009 11:32 AM
    Moderator