Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

lock (Account.All)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    lock (Account.All)

    Hello.
    I am trying to understand the purpose of the lock statement in the example under the NinjaScript Language Reference "Account" just underneath "// Find our Sim101 account."

    Thank you.

    Dan.

    #2
    Hello Dan,

    The lock() method is to prevent an object (in this case a collection of all accounts) from being accessed as it is being modified.

    More specifically:
    "The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released."

    In this case, this is to prevent a 'Collection was modified after the enumerator was instantiated' error.

    Below is a link to a forum post that discusses.
    https://ninjatrader.com/support/foru...06#post1099406

    Below is a link to the Microsoft documentation and to links to educational sites.
    https://docs.microsoft.com/en-us/dot...lock-statement
    https://stackoverflow.com/questions/...k-work-exactly
    https://stackoverflow.com/questions/...90569#23090569
    Last edited by NinjaTrader_ChelseaB; 08-01-2022, 08:00 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    113 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    60 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    40 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    43 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    82 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X