Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Brush property - Error on getting value for property.

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

    Brush property - Error on getting value for property.

    I have an indicator with a Brush property that is used to draw lines on the indicator panel with the specified color (brush).

    In the SetDefaults section of OnStateChange, if I set LineColor = Brushes.Red, indicator works fine.

    However, if I set using the FromRgb method,

    Code:
    LineColor = new SolidColorBrush(Color.FromRgb(100, 125, 150));
    the LineColor property shows the specified color in the settings tab. But when I click Apply / OK, I see the following error -

    Error on getting value for property 'LineColor'. The calling thread cannot access this object because a different thread owns it.


    I think the issue is with assigning SolidColorBrush to the property during initialization.

    How do I fix it ?


    Code:
            [NinjaScriptProperty]
            [XmlIgnore()]
            [Display(Name = "Line Color", Order = 1, GroupName = "Settings")]
            public Brush LineColor
            {
                get; set;
            }
            [Browsable(false)]
            public string LineColor_Serialize
            {
                get { return Serialize.BrushToString(this.LineColor); }
                set { this.LineColor = Serialize.StringToBrush(value); }
            }​
    Last edited by firstlanetech; 11-15-2024, 08:35 PM.

    #2
    I read under the documentation of "Creating a Custom Solid Color Brush", that the custom Solid Color Brush must be frozen using the Freeze().

    In my case using LineColor.Freeze() immediately solves the problem.

    In the OnStateChanged, in the SetDefaults section,

    Code:
    LineColor = new SolidColorBrush(Color.FromRgb(100, 125, 150));
    LineColor.Freeze();
    Is this the correct approach ? What happens if the user changes the LineColor in settings window ? Is the user specified brush frozen ?

    Comment


      #3
      Hello firstlanetech,

      Yes, this is the correct approach. The brushes are UI objects which exist on the UI thread which requires freezing to use in the NinjaScript thread.

      Below are links to the help guide.
      Help guide: NinjaScript > Educational Resources > Working with Brushes​ > Understanding custom brushes
      Help guide: NinjaScript > NinjaScript Best Practices​ > Error handling practices > Using WPF brushes

      "What happens if the user changes the LineColor in settings window ? Is the user specified brush frozen ?"

      Yes, the user selected color would be used. State.SetDefaults only sets the default value for the variable when the script is first constructed.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      78 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      63 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X