What happens if you break out of a Lock() statement?
Posted
by cyclotis04
on Stack Overflow
See other posts from Stack Overflow
or by cyclotis04
Published on 2010-05-17T20:15:41Z
Indexed on
2010/05/17
20:20 UTC
Read the original article
Hit count: 156
I'm writing a program which listens to an incoming TcpClient and handles data when it arrives. The Listen()
method is run on a separate thread within the component, so it needs to be threadsafe. If I break
out of a do
while
loop while I'm within a lock()
statement, will the lock be released? If not, how do I accomplish this?
Thanks!
(Any other advice on the subject of Asynchronous TCP Sockets is welcome as well.)
private void Listen()
{
do
{
lock (_client)
{
if (!_client.Connected) break;
lock (_stateLock)
{
if (!_listening) break;
if (_client.GetStream().DataAvailable) HandleData();
}
}
Thread.Sleep(0);
} while (true);
}
© Stack Overflow or respective owner