Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error - calling thread cannot access this object because a different thread owns it

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

    Error - calling thread cannot access this object because a different thread owns it

    I created a strategy in Ninjascript and got this error:

    ====

    Strategy 'myStrat01': Error on calling 'OnBarUpdate' method on bar 53216:

    The calling thread cannot access this object because a different thread owns it.

    ====

    I can't find a way to resolve it and tried different things but to no avail.

    Here is what i do...

    My strategy displays a series of buttons created during the initialization phase (State.DataLoaded phase).

    The error occurs when i try to change the background attribute of some of the buttons during the OnBarUpdate()

    Ex.

    protected override void OnBarUpdate()

    ...

    #region DoSomeCleanUp_WhenTradeIsOver

    //-

    if ( IsTradeInProgress && (atmStrategyId.Length + orderId.Length) == 0 )

    {

    IsTradeInProgress = false;

    isAtmStrategyCreated = false;

    buttonSell.Background = Brushes.DarkSlateGray; // <- this line raises an error

    sellModeEnabled = false;

    buttonBuy.Background = Brushes.DarkSlateGray; // <- this line raises an error

    buyModeEnabled = false;

    }

    }

    //-

    #endregion

    If i remove the faulty lines in Bold then everything works perfectly.

    Do you have something to suggest to change the background color during the OnBarUpdate. I tried to Freeze the colors but it did not help.

    Thanks

    #2
    Hi, thanks for posting.

    Use ChartControl.Dispatcher.InvokeAsync when accessing the button because the NinjaScript object runs on a non-UI thread:

    https://ninjatrader.com/support/foru...39#post1180339 - Example

    Kind regards,
    -ChrisL

    Comment


      #3
      Thank you very much, it worked as expected.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      81 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      149 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      79 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      52 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      59 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X