Calling my peeps for some help!

I have a bug that is kicking my rear and I know its a simple task/threading issue that I’m not remembering how to handle. I know someone here will see the answer in seconds. This is a UWP app.

If I let it run as-is then when the timer ticks sometimes SetLampColorAsync() does something and my bulbs change color. However, if I put a debug break in SetLampColorAsync() and step through it then it works every time. And sometimes once I’ve stepped through it a few times then it will work on its own for a long while. There’s something thread/task related that I’m forgetting. I’ve tried every arrangement of Task & Dispatcher functions that I can think of and nothing helps. Any ideas are greatly appreciated.


public sealed partial class MainPage : Page
    {
        // AllJoyn stuff
        private List<LampStateConsumer> _lampStateConsumers;

       ...

        public MainPage()
        {
            this.InitializeComponent();

            _lampStateConsumers = new List<LampStateConsumer>();

           ...

            var timer = new DispatcherTimer();
            timer.Tick += Timer_Tick;
            timer.Interval = new TimeSpan(0, 0, 5);
            timer.Start();
        }

        private async Task SetLampColorAsync(LampStateConsumer consumer)
        {
            var color = _cheerlights.Colors[_currentColor];
            await consumer.SetHueAsync(color.Hue);
            await consumer.SetSaturationAsync(color.Saturation);            
        }

        private async void Timer_Tick(object sender, object e)
        {
            var newColor = await _cheerlights.GetCurrentColorAsync();
            if (newColor == _currentColor) return;

            _currentColor = newColor;
            Debug.WriteLine(string.Format("@ cheerlights color is: {0}", _currentColor));

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () =>
                {
                    cheerlightsColorText.Text = _currentColor;
                    mainGrid.Background = new SolidColorBrush(_cheerlights.Colors[_currentColor].WindowsColor);
                });

            foreach (var lamp in _lampStateConsumers)
            {
                await SetLampColorAsync(lamp);
            }
        }
    }

I don’t see any obvious errors (doesn’t mean there aren’t any - just don’t see any on first reading), but, I do observe that if any one SetLampColorAsync throws, then the loop breaks. Have you tried running with first-chance exceptions enabled? In any case, I would wrap either the body or the call in a try/catch so that one failure doesn’t break the whole ‘frame’.

When SetLampColorAsync doesn’t do anything, do you still get your debug output every 5 sec or is that missing too?

What happens if you put debug output in the SetLampColorAsync? That would answer whether the routine is being called and is failing, or is just never being called anymore.

@ mcalsyn - thanks for taking a look and for the suggestions. I’d already tried all those things. However, you prompted me to try again and in the process I discovered the solution. I was originally controlling two LIFX bulbs. However, tonight I added a LED strip that is running an AllJoyn producer of my making. This app is supposed to turn all the lights it finds the same color. I noticed that my LED strip producer was working properly and changing color nearly 100% of the time while the LIFX bulbs were doing nothing. It’s a timing issue with the LIFX bulb firmware… Modifying the function to this causes everything to work properly. It’s still not great because everything doesn’t change color at the same time but its good enough for this demo.


        private async Task SetLampColorAsync(LampStateConsumer consumer)
        {
            var color = _cheerlights.Colors[_currentColor];
            await consumer.SetHueAsync(color.Hue);
            await Task.Delay(100);
            await consumer.SetSaturationAsync(color.Saturation);
            await Task.Delay(100);
        }