Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using custom Brush throwing exception

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

    Using custom Brush throwing exception

    Hello NinjaTrader support.
    I have the following code throwing exception: "The calling thread cannot access this object because a different thread owns it."
    Code:
    protected override void OnStateChange()
    ...
    if (State == State.SetDefaults)
    {
    ...
    [B]BLTrendColor = new SolidColorBrush(Color.FromRgb(0, 255, 0));
    BRTrendColor = new SolidColorBrush(Color.FromRgb(255, 0, 0));[/B]
    }
    
    protected override void OnBarUpdate()
    {
    if ((CurrentBar % 2) == 0)
    [B]BarBrush = BLTrendColor;[/B]
    else if ((CurrentBar % 3) == 0)
    [B]BarBrush = BRTrendColor;[/B]
    }
    
    [XmlIgnore]
    [Display(Name = "Bull trend", GroupName = "Color")]
    public Brush BLTrendColor { get; set; }
    
    [XmlIgnore]
    [Display(Name = "Bear trend", GroupName = "Color")]
    public Brush BRTrendColor { get; set; }
    Please help resolve the issue. Reason I am defining properties this way is that I need to collect the Brush from a user, store it and use it certain way. this is just a sample project (attached) to locate the issue.

    Note that I played extensively with built in Brushes, with properties being static etc. reaching out since ran out of options that I can think of.

    Thank you in advance!
    Attached Files
    Last edited by stasha; 04-24-2022, 09:48 AM. Reason: Brush

    #2
    Hello stasha,

    Try freezing the solid color brush.
    BLTrendColor = new SolidColorBrush(Color.FromRgb(0, 255, 0));
    BLTrendColor.Freeze();

    "Warning: If you do not call .Freeze() on a custom defined brush WILL eventually result in threading errors should you try to modify or access that brush after it is defined."


    "If you need to customize a new brush object, make sure to .Freeze() the brush before using it."
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by claxxical, 05-30-2017, 12:30 PM
    37 responses
    4,453 views
    0 likes
    Last Post Padan
    by Padan
     
    Started by SugarDefwebsite, Today, 02:18 AM
    0 responses
    3 views
    0 likes
    Last Post SugarDefwebsite  
    Started by usglucofreeze, Today, 01:19 AM
    0 responses
    7 views
    0 likes
    Last Post usglucofreeze  
    Started by f.saeidi, Today, 01:12 AM
    0 responses
    13 views
    0 likes
    Last Post f.saeidi  
    Started by NinjaTrader_ChelseaB, 03-14-2017, 10:17 AM
    227 responses
    34,319 views
    7 likes
    Last Post rare312
    by rare312
     
    Working...
    X