Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How would I resolve thread owning issue in OnBarUpdate?

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

    How would I resolve thread owning issue in OnBarUpdate?

    I'm newer to the UI things and would like the buttons to change color based on a trade direction, but I'm stuck on a thread issue where it cannot access the buttonsArray because a different thread owns it. I tried CurrentDispatcher.Invoke without any success. Any other ideas to get this to work?

    Code:
            protected override void OnBarUpdate()
            {
                if (buttonsArray == null)
                    return;
    
                if (Global.SelectedTradeDirection == Global.Direction.LONG)
                {
                    System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(() =>
                    {
                        buttonsArray[SHORT_BUTTON].Background = new SolidColorBrush(Colors.Transparent);
                    });
    
                    ForceRefresh();
                }
                else if (Global.SelectedTradeDirection == Global.Direction.SHORT)
                {
                    System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(() =>
                    {
                        buttonsArray[LONG_BUTTON].Background = new SolidColorBrush(Colors.Transparent);
                    });
    
                    ForceRefresh();
                }
                else
                {
                    System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(() =>
                    {
                        buttonsArray[LONG_BUTTON].Background = new SolidColorBrush(Colors.Transparent);
                        buttonsArray[SHORT_BUTTON].Background = new SolidColorBrush(Colors.Transparent);
                    });
    
                    ForceRefresh();
                }
            }​

    #2
    I resolved it.

    Code:
    protected override void OnBarUpdate()
            {
                if (buttonsArray == null)
                    return;
    
                if (buttonsArray[LONG_BUTTON] == null || buttonsArray[SHORT_BUTTON] == null)
                    return;
    
                if (Global.SelectedTradeDirection == Global.Direction.LONG)
                {
                    if (buttonsArray[SHORT_BUTTON].Dispatcher.CheckAccess())
                    {
                        ResetShortButton();
                    }
                    else
                    {
                        this.Dispatcher.InvokeAsync(new Action(() =>
                        {
                            ResetShortButton();
                        }));
                    }
    
                    ForceRefresh();
                }
                else if (Global.SelectedTradeDirection == Global.Direction.SHORT)
                {
                    if (buttonsArray[LONG_BUTTON].Dispatcher.CheckAccess())
                    {
                        ResetLongButton();
                    }
                    else
                    {
                        this.Dispatcher.InvokeAsync(new Action(() =>
                        {
                            ResetLongButton();
                        }));
                    }
    
                    ForceRefresh();
                }
                else
                {
                    if (buttonsArray[SHORT_BUTTON].Dispatcher.CheckAccess())
                    {
                        ResetLongButton();
                        ResetShortButton();
                    }
                    else
                    {
                        this.Dispatcher.InvokeAsync(new Action(() =>
                        {
                            ResetLongButton();
                        }));
    
                        this.Dispatcher.InvokeAsync(new Action(() =>
                        {
                            ResetShortButton();
                        }));
                    }
    
                    ForceRefresh();
                }
            }​
    Last edited by tonystarks; 11-11-2022, 08:24 AM.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    156 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    90 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    140 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    130 views
    1 like
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    107 views
    0 likes
    Last Post CarlTrading  
    Working...
    X