I have a background thread that sits waiting for new clients to connect to my TcpListener and when a new client connects it passes it off onto another worker thread and then loops straight back to waiting for another client. Like so:

vb.net Code:
  1. Do While DoListen
  2.      Threading.ThreadPool.QueueUserWorkItem(AddressOf ManageSession, SMTPListener.AcceptTcpClient())
  3. Loop

Thats all fine but I want to be able to cancel this 'listening' activity, which I cannot do as AcceptTcpClient is a blocking method. So when I call Stop() on the TcpClient instance I get the following exception:
A blocking operation was interrupted by a call to WSACancelBlockingCall
Obviously I could just handle the exception and ignore it but I'm sure there must be a more proper way of doing it. One thing I am thinking of trying is using the BeginAcceptTcpClient and EndAcceptTcpClient methods but for one thing I'm not sure if that will actually help and for another thing the Begin and End versions of methods have always confused me in the past... I mean for a start how do you know when to call End?

Anyway, I figured this must be a fairly common problem but searching the forum only turned up one result and it wasnt any use. I'll keep playing around and trawling google but just wondered if anyone had come across this before?