I use a nuget package called LifxNet by dotMorten. I can change the color of the bulb and turn it on and off. I now want to have a button that toggles the power of a bulb. I am trying to use the method GetLightStateAsync() in order to check if the light is on or off.
private void btnPower_Click(object sender, RoutedEventArgs e)
{
var powerState = client.GetLightStateAsync(selectedLight).Result.IsOn;
if (powerState == false)
{
client.SetDevicePowerStateAsync(selectedLight, true);
}
if (powerState == true)
{
client.SetDevicePowerStateAsync(selectedLight, false);
}
}
When I run this code the entire application hangs. When I pause the code it tells me that the line of code it’s up too is:
var powerState = client.GetLightStateAsync(selectedLight).Result.IsOn;
I have tried running it in different ways and I have figured out that it hangs when you add the .Result to the code. If you remove the .Result.IsOn and comment out the if statements then the program will run fine. I can’t figure out what is wrong and I would appreciate some help.