5

I have a thread that waits on TcpListener.AcceptTcpClient(), which blocks, which I want to suspend at times.

I've read about Monitor.Wait(...), but I only have experience working with mutexes and if the thread waits on a blocking method, it gets interesting.

Now that Thread.Suspend(...) is obsolete, how should I suspend the thread?

Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
  • 1
    If the thread is already blocked, then it makes no sense to suspend it. Could you please elaborate. – Brian Rasmussen Nov 27 '10 at 09:25
  • OK, I see what you mean. I don't want the thread to consume any incoming TCP connections and not use any resources when I "suspend" it. If this is already the case, would setting a `suspended` flag and waiting on it after `AcceptTcpClient` returns be the way to go? – Petrus Theron Nov 27 '10 at 09:55

1 Answers1

2

This is not possible, it is an unsolvable race condition. The listener could have accepted a connection a microsecond before you want to suspend it. Closing the listener so it won't accept any connections is the only sure way.

Rethink your logic here. Whatever it is going to do with that connection that makes you want to stop it probably needs to be locked instead.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536