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 NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    61 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    134 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X