Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to lock a method in class

how to lock a method in class

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    ting668
    wrote on last edited by
    #1

    Hi all, I want to lock a particular method of the object, so it allows only single access of the method at one time. But by using lock(this) to the method will cause the whole object to be locked, so other methods can't be used. What can I do to lock the particular method only ? Thanks

    R H 2 Replies Last reply
    0
    • T ting668

      Hi all, I want to lock a particular method of the object, so it allows only single access of the method at one time. But by using lock(this) to the method will cause the whole object to be locked, so other methods can't be used. What can I do to lock the particular method only ? Thanks

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Create a new meber variable (e.g. object lockVarForMethodA) and reference a unique object of your choice (e.g. new object()). Than use lock(lockVarForMethodA) in your method.

      1 Reply Last reply
      0
      • T ting668

        Hi all, I want to lock a particular method of the object, so it allows only single access of the method at one time. But by using lock(this) to the method will cause the whole object to be locked, so other methods can't be used. What can I do to lock the particular method only ? Thanks

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        ting668 wrote: But by using lock(this) to the method will cause the whole object to be locked, That's not true. The object is not locked in such a way that nothing else can use it. Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method). What Robert said is good, though. If you want to synchronize each method individually then create separate static instances of objects to lock against. These must be static so that they are the same object for all instances of your object in any thread within a single AppDomain. If you're going to synchronize each method individually and don't need instance data, I recommend you make the methods static. The CLR will synchronize static methods allowing only one thread of execution to enter at a time within a given AppDomain. This would be the same as locking against separate objects for each method yourself and makes for a better design and uses less memory (additional objects are not necessary). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

        T 1 Reply Last reply
        0
        • H Heath Stewart

          ting668 wrote: But by using lock(this) to the method will cause the whole object to be locked, That's not true. The object is not locked in such a way that nothing else can use it. Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method). What Robert said is good, though. If you want to synchronize each method individually then create separate static instances of objects to lock against. These must be static so that they are the same object for all instances of your object in any thread within a single AppDomain. If you're going to synchronize each method individually and don't need instance data, I recommend you make the methods static. The CLR will synchronize static methods allowing only one thread of execution to enter at a time within a given AppDomain. This would be the same as locking against separate objects for each method yourself and makes for a better design and uses less memory (additional objects are not necessary). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

          T Offline
          T Offline
          ting668
          wrote on last edited by
          #4

          HI "Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method)." That means if a class like this below: Class class1 { public void MethodA(){ lock(this) { ... } } public void MethodB() { ... } } Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? Is lock useless for single thread programming? Thanks

          H 1 Reply Last reply
          0
          • T ting668

            HI "Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method)." That means if a class like this below: Class class1 { public void MethodA(){ lock(this) { ... } } public void MethodB() { ... } } Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? Is lock useless for single thread programming? Thanks

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            ting668 wrote: Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? That is correct. Just try it. The majority of code snippets I post here in this forum I simply type in a simple text editor and compile on the command line, then run. Simple. ting668 wrote: Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? A single static method is synchronized by the CLR. If you want to synchronize different static methods you will need to use lock or some equivalent. ting668 wrote: Is lock useless for single thread programming? Yes. Within a single thread you can only execute one statement at a time. With that in mind there's no way you could execute the same method (or act on the same context, like getting or setting a serial number) at the same time. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

            T 1 Reply Last reply
            0
            • H Heath Stewart

              ting668 wrote: Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? That is correct. Just try it. The majority of code snippets I post here in this forum I simply type in a simple text editor and compile on the command line, then run. Simple. ting668 wrote: Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented? A single static method is synchronized by the CLR. If you want to synchronize different static methods you will need to use lock or some equivalent. ting668 wrote: Is lock useless for single thread programming? Yes. Within a single thread you can only execute one statement at a time. With that in mind there's no way you could execute the same method (or act on the same context, like getting or setting a serial number) at the same time. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

              T Offline
              T Offline
              ting668
              wrote on last edited by
              #6

              Hi Health, Class class1 { public void MethodA() { lock (this) { MethodB(); } } public void MethodB() { lock (this) { ... } } } From the code above, statements in locking block of MethodB still successfully run which is branched within the lock from MethodA. Why it is not a counter example of the locking theroy of the point1 : Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? of the last reply? Thanks

              H 1 Reply Last reply
              0
              • T ting668

                Hi Health, Class class1 { public void MethodA() { lock (this) { MethodB(); } } public void MethodB() { lock (this) { ... } } } From the code above, statements in locking block of MethodB still successfully run which is branched within the lock from MethodA. Why it is not a counter example of the locking theroy of the point1 : Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB? of the last reply? Thanks

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                Because it's executing within the same thread - not a different thread. Synchronization is not a replacement for state. You really should read Threading[^] in the .NET Framework SDK, as well as about the Monitor.Enter[^] method that the lock keyword (SyncLock in VB.NET) compiles to. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups