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