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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    43 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    29 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    46 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    38 views
    0 likes
    Last Post CarlTrading  
    Working...
    X