Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 is a link to the help guide. See the section 'Understanding custom brushes' > 'Creating a Custom Solid Color Brush'.​

    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.
    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 Touch-Ups, Today, 10:36 AM
      0 responses
      5 views
      0 likes
      Last Post Touch-Ups  
      Started by geddyisodin, 04-25-2024, 05:20 AM
      8 responses
      61 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by jxs_xrj, 01-12-2020, 09:49 AM
      4 responses
      3,289 views
      1 like
      Last Post jgualdronc  
      Started by Option Whisperer, Today, 09:55 AM
      0 responses
      5 views
      0 likes
      Last Post Option Whisperer  
      Started by halgo_boulder, 04-20-2024, 08:44 AM
      2 responses
      24 views
      0 likes
      Last Post halgo_boulder  
      Working...
      X