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 CaptainJack, 05-29-2026, 05:09 AM
    0 responses
    51 views
    0 likes
    Last Post CaptainJack  
    Started by CaptainJack, 05-29-2026, 12:02 AM
    0 responses
    31 views
    0 likes
    Last Post CaptainJack  
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    99 views
    0 likes
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    177 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    170 views
    0 likes
    Last Post CarlTrading  
    Working...
    X