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

System.Windows.Controls.Button

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

    System.Windows.Controls.Button

    Hi,

    I've created a button in chart trader (System.Windows.Controls.Button). Everything works OK, but I've decided to add a new feature. I want to change the Button's Context and Background as soon as a signal is triggered in OnBarUpdate.
    The code is:
    Code:
                public void SetSignal(SignalType val){
                    if (trSignalType != val){
                        trSignalType = val;
    
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            switch (trSignalType) {
    
                                case BUY:
                                    trButEntry.Background     = Brushes.Olive;
                                    trButEntry.Content         = "Long";
                                    break;
    
                                case SELL:
                                    trButEntry.Background     = Brushes.Red;
                                    trButEntry.Content         = "Short";
                                    break;
    
                                case NO:
                                    trButEntry.Background     = Brushes.Gray;
                                    trButEntry.Content         = "Is Disabled";
                                    break;
                            }
                        });
                    }
                }
    ​
    However, as soon as a signal is triggered - the Strategy is disabled an I see this error:
    HTML Code:
     (OnBarUpdate:332) System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.
       at System.Windows.Threading.Dispatcher.VerifyAccess()
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at NinjaTrader.NinjaScript.Strategies.Strategy1v1v0.Panel_ChartTrader.<SetSignal>b__29()
       at System.Windows.Threading.DispatcherOperation.InvokeDelegateCore()
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Windows.Threading.DispatcherOperation.Wait(TimeSpan timeout)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
       at System.Windows.Threading.Dispatcher.Invoke(Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
       at System.Windows.Threading.Dispatcher.Invoke(Action callback)
       at NinjaTrader.NinjaScript.Strategies.Strategy1v1v0.Panel_ChartTrader.SetSignal(SignalType val)
       at NinjaTrader.NinjaScript.Strategies.Strategy1v1v0.ActionEntry()
       at NinjaTrader.NinjaScript.Strategies.Strategy1v1v0.OnBarUpdate()​
    How can I solve the issue?

    #2
    Hello webus,

    Use the dispatcher from ChartControl (the UI thread).

    ChartControl.Dispatcher.InvokeAsync()
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Haiasi, 04-25-2024, 06:53 PM
    2 responses
    16 views
    0 likes
    Last Post Massinisa  
    Started by Creamers, Today, 05:32 AM
    0 responses
    5 views
    0 likes
    Last Post Creamers  
    Started by Segwin, 05-07-2018, 02:15 PM
    12 responses
    1,786 views
    0 likes
    Last Post Leafcutter  
    Started by poplagelu, Today, 05:00 AM
    0 responses
    3 views
    0 likes
    Last Post poplagelu  
    Started by fx.practic, 10-15-2013, 12:53 AM
    5 responses
    5,407 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Working...
    X