Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calling thread cannot access object because different thread owns it.

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

    Calling thread cannot access object because different thread owns it.

    I wrote an indicator. It runs in the main chart panel, plots objects and changes the bar color. For some reason, it started producing the error:

    Error on calling 'OnBarUpdate' method on bar 101: The calling thread cannot access this object because a different thread owns it.

    The second line of code here get the error:

    SolidColorBrush color = Brushes.Red;
    BarBrush = color;

    The indicator starts no new threads. It used to work correctly.

    I tried this:

    ChartControl.Dispatcher.InvokeAsync((() =>
    {
    BarBrush = color;
    }));

    The error doesn't occur, ​but only a few of the bars get colored. Which ones get colored seems random.

    Note: The indicator was written to be called by a strategy. It works when used that way. But when loaded it by itself it gets the error. But the indicator should be able to stand alone and used to work that way. For all intents, it should be a black box.





    Last edited by gbourque; 09-19-2023, 06:59 AM.

    #2
    Hello gbourque,

    I would suggest to make a small sample that demonstrates the problem and then attach that file so we can see what code is being used. You do not need to use dispatchers for normal NinjaScript code, dispatchers would be used if you are interacting with WPF controls in the user interface.

    Comment


      #3
      I imagine if I pare the indicator down to a small sample then whatever is causing the problem will go away, but I'll give it a try.

      Comment


        #4
        Okay, I narrowed things down. It seems to have something to do with creating a SolidColorBrush using Color.FromRgb.
        Comment out the color = LongColor line and it works. Uncomment it and it gets the error. What's weird is this works when the indicator is loaded by the strategy.
        NT Version 8.0.28.0 64-bit


        namespace NinjaTrader.NinjaScript.Indicators
        {
        public class aaaTest : Indicator
        {
        private string DisplayLabel = "aaaTest";

        private Brush LongColor;
        private Brush ShortColor;

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        Description = "";
        Name = "aaaTest";
        Calculate = Calculate.OnBarClose;
        MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
        PaintPriceMarkers = false;
        DisplayInDataBox = false;
        DrawOnPricePanel = true;
        IsOverlay = true;
        IsSuspendedWhileInactive = true;

        LongColor = new SolidColorBrush(Color.FromRgb(0,220,0));
        ShortColor = new SolidColorBrush(Color.FromRgb(220,0,0));
        }
        }

        protected override void OnBarUpdate()
        {
        Brush color = Brushes.Red;

        //color = LongColor;

        BarBrush = color;
        }
        }
        }​

        Comment


          #5
          Hello gbourque,

          Please try the following changes.

          Remove the code

          Code:
          Brush color = Brushes.Red;
          //color = LongColor;
          ​​

          Change the following lines to :

          Code:
          LongColor = new SolidColorBrush(Color.FromRgb(0,220,0));
          LongColor.Freeze();
          ShortColor = new SolidColorBrush(Color.FromRgb(220,0,0));
          ​ShortColor.Freeze();
          Then use the color directly:

          Code:
          BarBrush = LongColor;

          Comment


            #6
            I replaced all my FromRgb colors with predefined colors and it works now. I've had trouble with FromRgb before. I remember one time I used it in a text plot, and it changed the size of the font.

            Comment


              #7
              Brush.Freeze() huh? Okay, will do.

              Comment


                #8
                Hello gbourque,

                Without testing it would be hard to say if the freeze is the solution but you do need to freeze custom brushes after creating them so that may be related to the problem.

                Comment


                  #9
                  Freeze did work. Thanks!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  625 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  359 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  105 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  562 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  567 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X