Update programmatically image after Event with KNX.Net

48 views Asked by At

I'm creating a webapp to control some lights in my house. I can't understand why after firing the Event void, the image is not updating; while if i fire it from a button it is actually changing.

I have tried this so far, using KNX.Net libraries

...

    public void Event(string address, string state)
    {

        if (address.Equals(CH03LightOnOffAddressStatus) || address.Equals(CH04LightOnOffAddressStatus))
        {
            System.Diagnostics.Debug.WriteLine("New Event: device " + address + " has status (" + state + ") --> " + _connection.FromDataPoint("9.001", state));
        }
        else if (                
            address.Equals(CH01LightOnOffAddressStatus) ||                
            address.Equals(CH02LightOnOffAddressStatus)
            )
        {
            var data = string.Empty;

            if (state.Length == 1)
            {
                data = ((byte)state[0]).ToString();                    
            }
            else
            {
                var bytes = new byte[state.Length];
                for (var i = 0; i < state.Length; i++)
                {
                    bytes[i] = Convert.ToByte(state[i]);
                }

                data = state.Aggregate(data, (current, t) => current + t.ToString());
            }

            System.Diagnostics.Debug.WriteLine("New Event: device " + address + " has status --> " + data);
            condition = data;
            nowAddress = address;                               
        }

        if (condition == "1")
        {
            Image1.ImageUrl = @"\Res\Call.png";
        }
        else
        {
            Image1.ImageUrl = @"\Res\Bomb.png";
        }

    }

...

While if i fire it like this, the image is changing indeed

...

     private void CH01LightOn()
    {
        _connection.Action(CH01LightOnOffAddress, true);

        Thread.Sleep(500);

        if (condition == "1")
        {
            Image1.ImageUrl = @"\Res\Call.png";
        }
        else
        {
            Image1.ImageUrl = @"\Res\Bomb.png";
        }

    }

...

I just need the images to be changing after the event is fired. Thank you in advance.

1

There are 1 answers

0
Klaus Gütter On

This is probably a threading issue. If the Event method is not called on the UI thread, you cannot access your WinForms controls directly, but have to use Control.Invoke.

To try this, replace Image1.ImageUrl = @"\Res\Call.png" by:

if (Image1.InvokeRequired)
{
    Image1.Invoke((Action)(() => Image1.ImageUrl = @"\Res\Call.png"));
}
else
{
    Image1.ImageUrl = @"\Res\Call.png"
}