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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    52 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    24 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    16 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    23 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    24 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X