Announcement

Collapse
No announcement yet.

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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    587 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    341 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    103 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    555 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    552 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X