c# - What happens if you break out of a Lock() statement? -


I am writing a program that listens to incoming TcpClient and when it comes to handling data. The listen () method is run on a separate thread within the component, so it needs to be thressef. If I am inside a lock () statement, then break from to while will be locked out of the loop i lock will be issued? If not, how can I complete it?

Thank you!

(Any other advice on the topic of the asynchronous TCP socket is also welcome.)

  listen to private zero () (lock (_clant lock) {if (! _client .Connected break; lock (_State lock); if (_client.GetStream (.dateap available) handladata ();}} thread. Sleep (0);} while (true);}  

Yes the lock statement translates into a try / end section, for example, C # 4, such a lock statement:

  lock (obj) {// body}   

Broadly translates it: <: / P>

  bool lockWasTaken = false; var temp = obj; try {monitor.error (temp, ref lockWasTaken); {// body}} Finally {if (lockWasTaken) Monitor.Exit (temp);}  

When the execution leaves the scope of lock {} The built-in lock will be automatically released. Since the call to monitor, you will exit in any way (break / return / etc). The extension is wrapped, intermittently, in the end a try / Inside the block in play in.


Comments