Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

thread locking a shared method 5

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
What is thee preferred way to lock a method so that only one thread my access it at a time? Do I use the Monitor class within the method body, or do I just need to mark it with an Attribute? I can't seem to find any good examples in vb.net.

Thanks

[pipe]
Share your knowledge! -
 
Most of the threading stuff I've been working with lately doesn't worry about thread locking. There is one thread process that I added a check to that makes sure that the thread isn't already running befor starting it again (my process bar kept getting faster and faster!!) But I'm assuming you are looking for a way to block a calling thread so that it will be queued until the process is safe. If there is a way to do it I'm guessing it's going to be through using delegates to launch the process, and some sort of attribute that limits the process to 1 thread execution at a time. Just a guess.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
[banghead]
 
SyncLock on an object.
An allocated empty array is an object.


' Use SHARED variable as Lock for method
Private Shared m_OneOnly(-1) as Integer
Public Sub OneOnly()
SyncLock m_OneOnlyLock
........
End SyncLock
End Sub

- free online Compare/Diff of snippets
 
SyncLock on an object.
' An allocated empty array is an object.
' Use SHARED variable as Lock for method
Private Shared m_OneOnlyLock(-1) as Integer
Public Sub OneOnly()
SyncLock m_OneOnlyLock
........
End SyncLock
End Sub

- free online Compare/Diff of snippets
 
FYI:
SyncLock (and the lock keyword in C#) are aliases for the System.Monitor class. The aliases are easier to use, but if you like, you could do something like:
Code:
Public Shared Function Add() As Integer
  Try
    System.Monitor.Enter(Me)
    count += 1
    Add = count
  Finally
    System.Monitor.Exit(Me)
  End Try
End Function
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks to both of you for that. May come in handy.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Now I have more questions :) When using SynLock, is it necessary or even possible to use a try-finally so that the End Synclock is always called?

Now, isn't there an attribute that you can apply to a whole method that will do the same thing?



[pipe]
Share your knowledge! -
 
DragonWell.

Synclock has Try/Finally built-in.
"The SyncLock block is implicitly contained by a Try statement whose Finally block calls the Shared method System.Monitor.Exit on the expression. This ensures the lock is freed even when an exception is thrown. "

- free online Compare/Diff of snippets
 
There is a Synchonization Attribute but...

"Note There are two classes named SynchronizationAttribute: one in the System.Runtime.Remoting.Contexts namespace, and the other in the System.EnterpriseServices namespace. The System.EnterpriseServices.SynchronizationAttribute class supports only synchronous calls, and can be used only with serviced components. (For more information on serviced components, see Serviced Component Overview.) The System.Runtime.Remoting.Contexts.SynchronizationAttribute supports both synchronous and asynchronous calls, and can be used only with context bound objects. (For more information on context bound objects, see the ContextBoundObject class.)"


- free online Compare/Diff of snippets
 
JohnYingling said:
You can not use Me (a reference to an instance) in a Shared Method.
That'll teach me (a C# developer) to answer a VB.NET question! You're right, of course.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
You can't. I was being sloppy.

If you did need to lock an instance of yourself, you would have to pass "this" (or "Me") in as a parameter. The purpose of SyncLock and Monitor is to prevent other copies of yourself from modifying the protected variable at the same time you are. I've seen some people just create a New Object() and use that to lock on, but that won't work, as there's nothing preventing others from changing the protected variable (since everyone is getting a different instance of Object, and passing it to Enter()). You'd really need to pass in an instance of yourself for the lock to work with.
Code:
Public Shared Function Add(MyClass instance) As Integer
  Try
    System.Monitor.Enter(instance)
    count += 1
    Add = count
  Finally
    System.Monitor.Exit(instance)
  End Try
End Function
Or change the method to not be Shared. ;-)

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
But be careful.
I have handled a Voice Response Unit with 96 lines and therefore a possible 96 threads active and ALL the code that I had to write involving Synclock was to protect a shared resource from ALL access by other threads, not just from THIS instance operating on a different thread.

- free online Compare/Diff of snippets
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top