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. Enumerators and locks.

Enumerators and locks.

Scheduled Pinned Locked Moved C#
tutorialquestion
4 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.
  • J Offline
    J Offline
    jmhamm
    wrote on last edited by
    #1

    Does anyone know how to best lock access to the object during enumeration? Eg, is it a bad/good idea to use a Monitor.Enter in the constructor and a Monitor.Exit in the dispose function? I know, I could put a lock around, but I really need this to happen automatically. Cheers.

    B D 2 Replies Last reply
    0
    • J jmhamm

      Does anyone know how to best lock access to the object during enumeration? Eg, is it a bad/good idea to use a Monitor.Enter in the constructor and a Monitor.Exit in the dispose function? I know, I could put a lock around, but I really need this to happen automatically. Cheers.

      B Offline
      B Offline
      Bekjong
      wrote on last edited by
      #2

      Check out this [e-book]. It has some really nice examples on (automatic) locking and when (not) to use it in chapter 2. You might want to look at the Mutex class.

      Standards are great! Everybody should have one!

      J 1 Reply Last reply
      0
      • B Bekjong

        Check out this [e-book]. It has some really nice examples on (automatic) locking and when (not) to use it in chapter 2. You might want to look at the Mutex class.

        Standards are great! Everybody should have one!

        J Offline
        J Offline
        jmhamm
        wrote on last edited by
        #3

        Will do, thanks!

        1 Reply Last reply
        0
        • J jmhamm

          Does anyone know how to best lock access to the object during enumeration? Eg, is it a bad/good idea to use a Monitor.Enter in the constructor and a Monitor.Exit in the dispose function? I know, I could put a lock around, but I really need this to happen automatically. Cheers.

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          When you compile this code:

          IEnumerable<string> Test() {
          lock (someObj) {
          yield return "a";
          yield return "b";
          }
          }

          Then the C# compiler creates an enumerator class that calls Monitor.Enter on the first MoveNext call; and calls Monitor.Exit in the third MoveNext call, or in the Dispose method if Dispose is called between after the first MoveNext call and before the third. foreach will automatically dispose the enumerator, but I've seen people write code like

          IEnumerable<MyType> myCollection = ...;
          if (e.GetEnumerator.MoveNext()) { // if the collection is not empty

          So yes, using Monitor.Exit in the Dispose function is the way locking is meant to happen in enumerators, but make sure that your team is aware of the fact that enumerators must be disposed!

          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