Results 1 to 30 of 30

Thread: how to check if Mutex is Released

Hybrid View

  1. #1
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: how to check if Mutex is Released

    Quote Originally Posted by pacerier View Post
    i think the code u provided above would not work:
    Code:
     Get
                If InSet Then
                    SyncLock Lock
                        Return _SomeString
                    End SyncLock
                Else
                    Return _SomeString
                End If
            End Get
    because after you have Returned, everything else below it does not run. So the Lock never gets released. so what we have is this:
    Code:
     Get
                If InSet Then
                    SyncLock Lock
                    End SyncLock
                    Return _SomeString
                Else
                    Return _SomeString
                End If
            End Get
    No that is not the case, when you come out of the scope of a SyncLock block the lock will be released - it doesnt have to actually hit the End SyncLock line. Just like if you use a Using statement, you dont have to hit the End Using line for the object to be disposed, its just when you come out of the scope of that statement. I assume this behaviour is achieved by the fact that internally the SyncLock uses a Try/Catch/Finally block and releases the lock in the Finally block. You can see the same behaviour yourself if you just do something like this inside a Function:

    vb.net Code:
    1. Try
    2.     Dim something As String = "Some text"  
    3.     Return something
    4. Catch
    5.     MessageBox.Show("Oh dear")
    6. Finally
    7.     MessageBox.Show("Merry Christmas") 'seasonal examples FTW
    8. End Try
    Now if you run that function you will see that you get your 'something' object returned but the Finally block is still reached so you should still see the Merry Christmas message box
    Last edited by chris128; Dec 24th, 2009 at 06:54 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2009
    Posts
    376

    Re: how to check if Mutex is Released

    Quote Originally Posted by chris128 View Post
    No that is not the case, when you come out of the scope of a SyncLock block the lock will be released - it doesnt have to actually hit the End SyncLock line. Just like if you use a Using statement, you dont have to hit the End Using line for the object to be disposed, its just when you come out of the scope of that statement. I assume this behaviour is achieved by the fact that internally the SyncLock uses a Try/Catch/Finally block and releases the lock in the Finally block. You can see the same behaviour yourself if you just do something like this inside a Function:
    what if i had something like this:

    Code:
    1:                              SyncLock Lock
                                        GoTo 1
                                    End SyncLock
    does the thread leaves synclock when it hits GoTo 1 or does he enters the lock 2 times, den 3, den 4 ..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width