I want to listen for incoming socket connections on a thread. When I call MySocket.BeginAccept or MySocket.Accept, I cannot figure out how to stop the thread. Is there no way to stop the Socket Accept?
Define a public variabl mbol_Shutdown. Next when you are ready to shut the
socket down, assign the global mbol_Shutdown variable to True and signal the
System.Threading.ManualResetEvent "mobj_ListenResetEvent"
(mobj_ListenResetEvent.Set()). This should allow you to break out of the
loop.
While True
mobj_ListenResetEvent.Reset()
mobj_Socket.BeginAccept(New AsyncCallback(AddressOf
AcceptCallback), mobj_Socket)
mobj_ListenResetEvent.WaitOne()
If mbol_Shutdown then Exit While
End While
'clean up
Dennis said:
I want to listen for incoming socket connections on a thread. When I call
Define a public variabl mbol_Shutdown. Next when you are ready to shut the
socket down, assign the global mbol_Shutdown variable to True and signal the
System.Threading.ManualResetEvent "mobj_ListenResetEvent"
(mobj_ListenResetEvent.Set()). This should allow you to break out of the
loop.
While True
mobj_ListenResetEvent.Reset()
mobj_Socket.BeginAccept(New AsyncCallback(AddressOf
AcceptCallback), mobj_Socket)
mobj_ListenResetEvent.WaitOne()
If mbol_Shutdown then Exit While
End While
Slightly cleaner would be to make the while construct *itself* test the
flag. However, you should also synchronize access to the flag to make
sure you don't just get a cached version each iteration. You could do
that by making the flag volatile, or by setting/reading a property
which contains a lock.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.