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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      86 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      125 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      64 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      117 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      67 views
      0 likes
      Last Post PaulMohn  
      Working...
      X