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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Lock(This)

Lock(This)

Scheduled Pinned Locked Moved C#
questionhelptutorial
11 Posts 5 Posters 2 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.
  • J Offline
    J Offline
    jpk420
    wrote on last edited by
    #1

    Hello All , I was readinf the lock concept and find that there is problem with lock(this) or lock(public type). what is the problem doing like that ? Please explain me with an example

    E L 2 Replies Last reply
    0
    • J jpk420

      Hello All , I was readinf the lock concept and find that there is problem with lock(this) or lock(public type). what is the problem doing like that ? Please explain me with an example

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      A good starting point for research is the documentation[^]. I think you would be surprised at the information available in MSDN.

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

      L 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        A good starting point for research is the documentation[^]. I think you would be surprised at the information available in MSDN.

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Nah. MSDN documentation is like holiday brochures without pictures, nothing interesting there. :laugh:

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        E J 2 Replies Last reply
        0
        • L Luc Pattyn

          Nah. MSDN documentation is like holiday brochures without pictures, nothing interesting there. :laugh:

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          Actually, the particular article I referenced while sparse does indicate the common problems and a common solution in the remarks section. Although, most MSDN is total crap.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

          1 Reply Last reply
          0
          • L Luc Pattyn

            Nah. MSDN documentation is like holiday brochures without pictures, nothing interesting there. :laugh:

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            J Offline
            J Offline
            jpk420
            wrote on last edited by
            #5

            I check the documentation . I did not get that what was the problem . any way we are making lock on that object . So what matters if that is private or public? Please help me out

            L 1 Reply Last reply
            0
            • J jpk420

              I check the documentation . I did not get that what was the problem . any way we are making lock on that object . So what matters if that is private or public? Please help me out

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              you could as well ask why private and protected members exist at all. Why not make everything public? it is a matter of encapsulation, separation of concern, improving reliability, etc. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              J 1 Reply Last reply
              0
              • L Luc Pattyn

                you could as well ask why private and protected members exist at all. Why not make everything public? it is a matter of encapsulation, separation of concern, improving reliability, etc. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                J Offline
                J Offline
                jpk420
                wrote on last edited by
                #7

                No That i not mean at all . what i got confused is anyhow i am locking that object . So untill i done with my work no body can tamper it with . So even if i declare private or public i am holding a lock on that . So how that acces specifier will affect that . Sorry to trouble u .. bUt a good example will clear my doubt .. my brain is not able to leav this doubt unanswered.

                L 1 Reply Last reply
                0
                • J jpk420

                  No That i not mean at all . what i got confused is anyhow i am locking that object . So untill i done with my work no body can tamper it with . So even if i declare private or public i am holding a lock on that . So how that acces specifier will affect that . Sorry to trouble u .. bUt a good example will clear my doubt .. my brain is not able to leav this doubt unanswered.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  when your locker object is public I can lock it without your code being aware of it; so all your code would break right away. Don't make public what doesn't have to be public, that is one of the OO principles. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  P 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    when your locker object is public I can lock it without your code being aware of it; so all your code would break right away. Don't make public what doesn't have to be public, that is one of the OO principles. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Yet with Reflection I can see your privates. :cool:

                    L 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Yet with Reflection I can see your privates. :cool:

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      You'd better think twice before applying reflection on me... :-\

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                      1 Reply Last reply
                      0
                      • J jpk420

                        Hello All , I was readinf the lock concept and find that there is problem with lock(this) or lock(public type). what is the problem doing like that ? Please explain me with an example

                        L Offline
                        L Offline
                        Leonardo Muzzi
                        wrote on last edited by
                        #11

                        The problem with Lock(this) and Lock(type) is that these objects are public, so an external code could lock on them. So if your class is named YourClass, I can make a code like this:

                        YourClass obj = new YourClass();
                        lock(obj){ ... }

                        And that would probably deadlock your internal code. Same problem for a public type. Since it is commom to lock an object that you want to protect, it's not too dificult for a code like this to happen. The best approach is to create an object just for locking on it, instead of locking the class object. Make this locking object "static" and you get the same effect of locking a type.

                        Regards, Leonardo Muzzi

                        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