• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unhandled Exception

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

    Unhandled Exception

    I have had little problem with managing threads lately.

    Now I get this message which seems to come from the ContextMenu elements that I have added.

    The message read like this :

    Unhandled exception : Element already has a logical parent. It must be detached from the old parent before it is attached to a new one.

    I read the message but can't figure out what it exactly means. Have you gone through this before ?

    Thank you

    #2
    Hello blar58,

    Thank you for your post.

    The message indicates the object is already attached to a parent object (such as a button to a grid) and it appears an attempt to assign the object to a new parent object is attempted but the object has not been detached from the prior.

    There are several discussions on the web concerning this message but it is specific to the code used. In general the description above is about as basic as it can be made. A quick Google search provides several different use case examples: https://www.google.com/webhp?sourcei...d+to+a+new+one

    If you would like us to take a look please attach the relevant NinjaScript object to your response.
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      OK I found where the problem is but don't know what is wrong.

      I took the example you have given on this forum for adding items to the Context Menu.

      Here is my code :

      Variables declaration :

      Code:
      private System.Windows.Controls.Separator splitSeparator;
      private System.Windows.Controls.MenuItem fullContextMenuItem;
      private System.Windows.Controls.MenuItem splitContextMenuItem;
      private bool doOnce = true;
      On state == State.Configure :
      Code:
      splitSeparator = new Separator();
      splitContextMenuItem = new System.Windows.Controls.MenuItem() {Header = "Split" };
      splitContextMenuItem.Click += splitContextMenuItem_Click;
      fullContextMenuItem = new System.Windows.Controls.MenuItem() {Header = "Full" };
      fullContextMenuItem.Click += fullContextMenu_Click;
      On State == State.Terminated :
      Code:
      if( ChartControl != null )
      {		
         if( splitSeparator != null )
            ChartControl.ContextMenu.Items.Remove(splitSeparator);
      					
         if( fullContextMenuItem != null )
           ChartControl.ContextMenu.Items.Remove(fullContextMenuItem);
      					
         if( splitContextMenuItem != null )
           ChartControl.ContextMenu.Items.Remove(splitContextMenuItem);
      }
      Now the problem comes here on that final step From OnBarUpdate :
      Code:
      if(doOnce)
      {
         if( ChartControl != null )
         {
            ChartControl.Dispatcher.BeginInvoke( new Action(() =>
         {
                ChartControl.ContextMenu.Items.Add(splitSeparator);
                ChartControl.ContextMenu.Items.Add(fullContextMenuItem);
                ChartControl.ContextMenu.Items.Add(splitContextMenuItem);
                doOnce = false;
         }));
      
        }
      }
      On the trace file it is said that the error message comes from that final step coming form the OnBarUpdate method.


      Thanks

      Comment


        #4
        Originally posted by blar58 View Post

        On the trace file it is said that the error message comes from that final step coming form the OnBarUpdate method.


        Thanks
        Your "DoOnce" flag is being set to false within the async action to OnBarUpdate (through BeginInvoke(), which is causing the action to be called repeatedly.

        1. Move the flag outside of the dispatcher action
        2. Or you could simplify to only set your property on CurrentBar == 0,
        3. Or just do this in State.Historical which is only called once throughout the lifetime of the script.

        Solution 1 - Move the flag outside of the dispatcher action
        Code:
        if (doOnce)
        {
        	if ( ChartControl != null )
        	{
        		ChartControl.Dispatcher.BeginInvoke( new Action(() =>
        		{
        			ChartControl.ContextMenu.Items.Add(splitSeparator);
        			ChartControl.ContextMenu.Items.Add(fullContextMenuItem);
        			ChartControl.ContextMenu.Items.Add(splitContextMenuItem);
        
        		}));
        	}
        	doOnce = false; // set outside of the dispatcher which action runs ascyrnconsly to OnBarUpdate
        }
        Solution 2 - Or you could simplify to only set your property on CurrentBar == 0,

        Code:
        if (CurrentBar == 0) // only apply action on first bar on chart
        {
        	if ( ChartControl != null )
        	{
        		ChartControl.Dispatcher.BeginInvoke( new Action(() =>
        		{
        			ChartControl.ContextMenu.Items.Add(splitSeparator);
        			ChartControl.ContextMenu.Items.Add(fullContextMenuItem);
        			ChartControl.ContextMenu.Items.Add(splitContextMenuItem);
        		}));
        	}
        }

        Solution 3 - Or...You can just do this in OnStateChange during State.Historical

        Code:
        else if (State == State.Historical) // only called once throughout indicator lifetime.
        {
        	if (ChartControl != null)
        	{
        		ChartControl.Dispatcher.BeginInvoke( new Action(() =>
        		{
        			ChartControl.ContextMenu.Items.Add(splitSeparator);
        			ChartControl.ContextMenu.Items.Add(fullContextMenuItem);
        			ChartControl.ContextMenu.Items.Add(splitContextMenuItem);
        		}));
        	}
        }
        Last edited by NinjaTrader_Matthew; 04-27-2016, 02:55 PM.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Matthew you are great !

          Makes lot of sense. Thank you very much. Much appreciated

          Thanks!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Dr3am, Today, 05:10 PM
          0 responses
          1 view
          0 likes
          Last Post Dr3am
          by Dr3am
           
          Started by imalil, Today, 04:28 PM
          0 responses
          2 views
          0 likes
          Last Post imalil
          by imalil
           
          Started by bboyle1234, Today, 03:47 PM
          0 responses
          1 view
          0 likes
          Last Post bboyle1234  
          Started by caveat_lector, Today, 03:46 PM
          0 responses
          5 views
          0 likes
          Last Post caveat_lector  
          Started by fx.practic, Today, 03:29 PM
          0 responses
          3 views
          0 likes
          Last Post fx.practic  
          Working...
          X