Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,158 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,125 people online right now. Registration is fast and FREE... Join Now!




lock(object) {} and return

 
Reply to this topicStart new topic

lock(object) {} and return

Nayana
12 Dec, 2007 - 12:31 AM
Post #1

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Hello,

I was just wondering if anyone knows offhand whether, for example

CODE

lock(anObject) {
  ...
  return false;
  ...
}


Whether the monitor would be released when the return false; command was executed.

???
User is offlineProfile CardPM
+Quote Post

itpro4470
RE: Lock(object) {} And Return
12 Dec, 2007 - 07:08 PM
Post #2

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 120



Thanked: 1 times
Dream Kudos: 25
My Contributions
not sure off hand if that will work, What I would do is enclose it in an endless loop then break out of the loop when you want to ie...
CODE

while(true)
{
    lock(resource)
    {
        .....
        return true;
        break;
        .....
        break;
    }
}

User is online!Profile CardPM
+Quote Post

Nayana
RE: Lock(object) {} And Return
12 Dec, 2007 - 08:28 PM
Post #3

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
Hmm, if I'm not lazy I'll have a look at what the IL says...

Maybe it would be better just to handle the monitor myself. But that doesn't look as nice sad.gif
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Lock(object) {} And Return
13 Dec, 2007 - 02:49 PM
Post #4

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 342



Thanked: 10 times
Dream Kudos: 100
My Contributions
This should exit properly.
I ran code through ildasm that had a lock
statement in it. If you look carefully at the IL code
the lock is transformed into a try/finally block.
So any exiting should trigger the finally block


CODE

// Class with lock
    public class Alpha
    {
        public void Beta()
        {
            lock (this)
            {
                while (true)
                    Console.WriteLine("Alpha.Beta is runnin in its own thread");
            }
        }
    };


CODE

// IL Code
.method public hidebysig instance void  Beta() cil managed
{
  // Code size       36 (0x24)
  .maxstack  2
  .locals init ([0] class LibTester.learnThreading.Alpha CS$2$0000,
           [1] bool CS$4$0001)
  IL_0000:  nop
  IL_0001:  ldarg.0
  IL_0002:  dup
  IL_0003:  stloc.0
  IL_0004:  call       void [mscorlib]System.Threading.Monitor::Enter(object)
  IL_0009:  nop
  .try
  {
    IL_000a:  nop
    IL_000b:  br.s       IL_0018
    IL_000d:  ldstr      "Alpha.Beta is runnin in its own thread"
    IL_0012:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_0017:  nop
    IL_0018:  ldc.i4.1
    IL_0019:  stloc.1
    IL_001a:  br.s       IL_000d
  }  // end .try
  finally
  {
    IL_001c:  ldloc.0
    IL_001d:  call       void [mscorlib]System.Threading.Monitor::Exit(object)
    IL_0022:  nop
    IL_0023:  endfinally
  }  // end handler
} // end of method Alpha::Beta


User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 11:26PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month