Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error:The calling thread cannot access this object because a different thread owns it

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

    Error:The calling thread cannot access this object because a different thread owns it

    Hello
    I am working with a custom drawing tool. I am encountering this error and I think I have narrowed it down to the SolidColorBrush I am using to fill the anchor points. I have read on various forum threads and also on the help guide that the .Freeze() method works to fix this problem. The examples in the help guide work well with brushes created in Strategies and Indicators, however I am writing a Drawing Tool that uses SharpDX objects for rendering. Also, the SolidColorBrush is adjustable in the UI if the user wishes to change the look of the anchors. I will provide a snippet of how the SolidColorBrush is created and used and I would greatly appreciate another set of eye to see why this error is occurring and where the .Freeze() method would fit in.

    Code:
    public class MyDrawingTool: DrawingTool
    {
      private SolidColorBrush anchorFillColor = new SolidColorBrush();
    
      //Set Anchor Fill Color
      [NinjaScriptProperty]
      [XmlIgnore]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Anchor Fill", GroupName = "Anchors", Order = 13)]
      public SolidColorBrush anchorFill
      {
        get { return anchorFillColor; }
        set { anchorFillColor = value; }
      }
    
      //Serialize the anchorFill Color
      [Browsable(false)]
      public string anchorFillColorSerializable
      {
        get { return anchorFill.ToString(); }
        set { anchorFill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(value)); }
      }
    
      if (State.Defaults)
      {
        anchorFillColor = Brushes.White;
      }
    
      OnRender()
      {
        var mediaColor = anchorFill.Color;
        var dxColor = new SharpDX.Color4(mediaColor.ScR, mediaColor.ScG, mediaColor.ScB, mediaColor.ScA);
      
        SharpDX.Direct2D1.SolidColorBrush customDXBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, dxColor);
    
        RenderTarget.... //Render Anchor Points​
      }
    }
    Last edited by Don22Trader1; 03-06-2024, 11:36 AM.

    #2
    Hello Don22Trader1,

    Thank you for your note.

    With SolidColorBrushes you need to .Freeze() these.
    Below are links to the help guide.
    Help guide: NinjaScript > Educational Resources > Working with Brushes​ > Understanding custom brushes
    Help guide: NinjaScript > NinjaScript Best Practices​ > Error handling practices > Using WPF brushes

    The new SolidColorBrush() will need to be instantiated in OnStateChange() during State.SetDefaults.

    Code:
    private SolidColorBrush myBrush;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    myBrush = new SolidColorBrush(Brushes.Green.Color);
    }
    }
    
    public SolidColorBrush MyBrush
    {
    get { return myBrush; }
    set
    {
    myBrush = value;
    if (myBrush != null)
    {
    if (myBrush.IsFrozen)
    myBrush = myBrush.Clone();
    myBrush.Freeze();
    }
    }
    }
    
    [Browsable(false)]
    public string MyBrushSerialize
    {
    get { return Serialize.BrushToString(myBrush); }
    set { myBrush = (SolidColorBrush)Serialize.StringToBrush(value); }
    }​​
    Also, these examples show using brushes from UI properties and converting these to SharpDX brushes.
    https://forum.ninjatrader.com/forum/ninjatrader-8/indicator-development/98639-performance-optimization?p=789606#post789606​
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea B,

      I implemented the logic you provided and the tool did not work properly. However, after tinkering with it I changed this logic:

      Code:
      //~Chelsea B.
      [Browsable(false)]
      public string MyBrushSerialize
      {
      get { return Serialize.BrushToString(myBrush); }
      set { myBrush = (SolidColorBrush)Serialize.StringToBrush(value); }
      }
      I reverted back to what I had originally:

      Code:
      //~Don22Trader1
      [Browsable(false)]
      public string anchorFillColorSerializable
      {
      get { return anchorFill.ToString(); }
      set { anchorFill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(value)); }
      }
      This reversion, paired with the other .Freeze() logic you provided did the trick and now I am able to have my tool used on separate windows without having that error occur. Thank you!
      Last edited by Don22Trader1; 03-06-2024, 01:34 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      649 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      370 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      576 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X