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 CarlTrading, 03-31-2026, 09:41 PM
    1 response
    87 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    47 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    66 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    69 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    59 views
    0 likes
    Last Post CarlTrading  
    Working...
    X