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:
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;
}
});
}
}
(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()

Comment