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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    36 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    14 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    20 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    22 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X