Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

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

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

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

    I have seen this error a lot and know some are my issue trying to access the wrong thing after it is initialized. But here I am trying to access the PlotBrushes[] and seem to get the error that follows: (Note Displacement=0 )
    Code:
    if (sessionBar == 1 && gap0)
     PlotBrushes[0][-Displacement] = Brushes.Transparent;
     elseif (SessionVWAP[0] > SessionVWAP[1])
     PlotBrushes[0][-Displacement] = upBrush;
     elseif (SessionVWAP[0] < SessionVWAP[1])
     PlotBrushes[0][-Displacement] = downBrush;
     elseif(sessionBar == 2 && gap0)
     PlotBrushes[0][-Displacement] = upBrush;
     else
    PlotBrushes[0][-Displacement] = PlotBrushes[0][1-Displacement];
     if(sessionBar == 1 && gap1)
     {
     for (int i = 1; i <= 6; i++)
     PlotBrushes[i][-Displacement] = Brushes.Transparent;
     }
    Here is the error produced, and note, it isn't all the time.... That is the part making this debugging difficult.
    System.InvalidOperationException occurred
    HResult=-2146233079
    Message=The calling thread cannot access this object because a different thread owns it.
    Source=WindowsBase
    StackTrace:
    at System.Windows.Threading.DispatcherObject.VerifyAc cess()
    at System.Windows.Freezable.Freeze(Boolean isChecking)
    at NinjaTrader.NinjaScript.BrushSeries.Set(Int32 index, Brush value)
    at NinjaTrader.NinjaScript.Indicators.anaCurrentDayVW APV43.OnBarUpdate() in c:\Users\Greg\Documents\NinjaTrader 8\bin\Custom\Indicators\anaCurrentDayVWAPV43.cs:li ne 706
    InnerException:

    So is this assignment not allowed!?!?
    Attached Files

    #2
    Hello NJA_MC,

    Thank you for your post.

    This assignment is allowed, it appears that the UI thread is not holding the NinjaScript object and somewhere we are seeing an issue is calling the brush to the UI or vice versa.

    Would you mind sending over your workspaces and the indicator you are testing to platformsupport[at]ninjatrader[dot]com?

    Comment


      #3
      Originally posted by NJA_MC View Post
      I have seen this error a lot and know some are my issue trying to access the wrong thing after it is initialized. But here I am trying to access the PlotBrushes[] and seem to get the error that follows: (Note Displacement=0 )
      Code:
      if (sessionBar == 1 && gap0)
       PlotBrushes[0][-Displacement] = Brushes.Transparent;
       elseif (SessionVWAP[0] > SessionVWAP[1])
       PlotBrushes[0][-Displacement] = upBrush;
       elseif (SessionVWAP[0] < SessionVWAP[1])
       PlotBrushes[0][-Displacement] = downBrush;
       elseif(sessionBar == 2 && gap0)
       PlotBrushes[0][-Displacement] = upBrush;
       else
      PlotBrushes[0][-Displacement] = PlotBrushes[0][1-Displacement];
       if(sessionBar == 1 && gap1)
       {
       for (int i = 1; i <= 6; i++)
       PlotBrushes[i][-Displacement] = Brushes.Transparent;
       }
      Here is the error produced, and note, it isn't all the time.... That is the part making this debugging difficult.
      System.InvalidOperationException occurred
      HResult=-2146233079
      Message=The calling thread cannot access this object because a different thread owns it.
      Source=WindowsBase
      StackTrace:
      at System.Windows.Threading.DispatcherObject.VerifyAc cess()
      at System.Windows.Freezable.Freeze(Boolean isChecking)
      at NinjaTrader.NinjaScript.BrushSeries.Set(Int32 index, Brush value)
      at NinjaTrader.NinjaScript.Indicators.anaCurrentDayVW APV43.OnBarUpdate() in c:\Users\Greg\Documents\NinjaTrader 8\bin\Custom\Indicators\anaCurrentDayVWAPV43.cs:li ne 706
      InnerException:

      So is this assignment not allowed!?!?
      How are you declaring your upBrush.. with Brushes.Color or with new SolidBrush?

      I'm running into the same issue/error with the calling thread.. I'm trying to declare a Brush by using new SolidBrush vs Brushes.Color with a drawobject.. I'm doing this as I want to be able to use ARGB and/or RGB for defining the color..

      Here is simple script.. Drawing/ReDrawing a Dot at the high of everybar.. One bool property to choose between using SolidBrush or Brushes.. Throw this on one min chart.. At the close of the first real time bar you will get a calling thread error, as well as on every subsequent bar.. (I'm not sure why this only throws an error after the close of the first real time bar, as in my other project, it's throwing an error historically as well) Now after confirming the exception, Go into properties and turn off the useSolidBrush bool.. There is not problem when using the standard Brush..

      Should this be expected? or is this not allowed? or am I doing something wrong?


      Attached Files
      -=Edge=-
      NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

      Comment


        #4
        Originally posted by -=Edge=- View Post
        How are you declaring your upBrush.. with Brushes.Color or with new SolidBrush?

        I'm running into the same issue/error with the calling thread.. I'm trying to declare a Brush by using new SolidBrush vs Brushes.Color with a drawobject.. I'm doing this as I want to be able to use ARGB and/or RGB for defining the color..

        Here is simple script.. Drawing/ReDrawing a Dot at the high of everybar.. One bool property to choose between using SolidBrush or Brushes.. Throw this on one min chart.. At the close of the first real time bar you will get a calling thread error, as well as on every subsequent bar.. (I'm not sure why this only throws an error after the close of the first real time bar, as in my other project, it's throwing an error historically as well) Now after confirming the exception, Go into properties and turn off the useSolidBrush bool.. There is not problem when using the standard Brush..

        Should this be expected? or is this not allowed? or am I doing something wrong?


        Try freezing the new SolidBrush() first.

        Comment


          #5
          Originally posted by koganam View Post
          Try freezing the new SolidBrush() first.
          Interesting... kind of a pain for all the colors I declare, but that works!

          Thank You Very Much!


          -=Edge=-
          NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

          Comment


            #6
            Originally posted by -=Edge=- View Post
            Interesting... kind of a pain for all the colors I declare, but that works!

            Thank You Very Much!


            We probably need to have this in the NT documentation? No?

            Comment


              #7
              Originally posted by koganam View Post
              We probably need to have this in the NT documentation? No?
              Or a simple reference sample.. Would have definitely saved me some time trying to debug that.. Simple fix though, thanks again!


              -=Edge=-
              NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

              Comment


                #8
                I had to Google freezing to see what was going on.
                Came across this, which looks like it applies to here?

                Learn how to use and create freezable objects in Windows Presentation Foundation (WPF) applications.

                Comment


                  #9
                  Originally posted by koganam View Post
                  Try freezing the new SolidBrush() first.
                  Wow!

                  This is a biggie... I tried this and think it may have fixed a lot of other intermittent issues. This is important to discuss in the NinjaScript reference!

                  "Freezing" the brushes seems to have cleaned up and closed this issue.

                  Comment


                    #10
                    Grrrr...

                    Okay, I am not getting the same error, getting a different one intermittently... So not quite out of the woods yet:

                    Code:
                     2015-06-03 16:26:05:034 ERROR: Failed to call 'Add' method: Write lock may not be acquired with read lock held. This pattern is prone to deadlocks. Please ensure that read locks are released before taking a write lock. If an upgrade is necessary, use an upgrade lock in place of the read lock.

                    Comment


                      #11
                      Went though this with Development and Product Management team was just educated that yes we should be Freezing brushes due to the multi-threaded nature of NinjaTrader.

                      This has impact with DrawingTools, Setting Plots, Setting Lines, and OnRender method.

                      I will start adding documentation this week to make this more clear, thanks guys for running through this.

                      Comment


                        #12
                        Furthermore, I will ask development to terminate the NS and log an error explaining to you that you needed to freeze the brush if a non-freezed brush is passed in. Which should help for future cases as well.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Brett View Post
                          Furthermore, I will ask development to terminate the NS and log an error explaining to you that you needed to freeze the brush if a non-freezed brush is passed in. Which should help for future cases as well.
                          Hm. That may be overkill. It is only necessary to freeze brushes that may possibly be accessed across threads. If the brush is confined to one thread, such as being defined in OnRender() for example, there may be no need to freeze it.

                          This becomes especially important if we intend to change the color or any other properties of the brush, (as for example, drawing multiple lines of text that have different colors or backgrounds). Granted that we can always Clone() the brush and so in a roundabout fashion change the brush properties, it may sometimes simply be preferable to modify the brush directly.

                          Just my $0.02.
                          Last edited by koganam; 07-21-2016, 11:17 AM. Reason: Corrected spelling.

                          Comment


                            #14
                            Problem is not one thread is used in the majority of situations.

                            OnBarUpdate, OnStateChanged, OnMarketData, OnMarketDepth is all multi-threaded. Sure the render call is the only one thats not but its better to freeze then have the majority of clients run into intermittent issues is current thinking.

                            Comment


                              #15
                              Plus if your in OnRendner you've committed to handling things yourself anyways. Could always handle yourself instead. (Don't use Draw. methods etc.)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by terofs, Yesterday, 04:18 PM
                              1 response
                              21 views
                              0 likes
                              Last Post terofs
                              by terofs
                               
                              Started by CommonWhale, Today, 09:55 AM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by Gerik, Today, 09:40 AM
                              2 responses
                              7 views
                              0 likes
                              Last Post Gerik
                              by Gerik
                               
                              Started by RookieTrader, Today, 09:37 AM
                              2 responses
                              12 views
                              0 likes
                              Last Post RookieTrader  
                              Started by alifarahani, Today, 09:40 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X