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