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 kinfxhk, 07-14-2026, 09:39 AM
      0 responses
      15 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 10:18 AM
      0 responses
      59 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 09:50 AM
      0 responses
      42 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 07:21 AM
      0 responses
      47 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-11-2026, 02:11 AM
      0 responses
      38 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Working...
      X