Problems with ConnectionState in AsterNet

39 views Asked by At

I have next code:

using AsterNET.Manager;
using AsterNET.Manager.Event;

private ManagerConnection manager;
private bool StartManager()
        {
            manager = new(
                Properties.Resources.amiAddress,
                Int32.Parse(Properties.Resources.amiPort),
                Properties.Resources.amiLogin,
                Properties.Resources.amiPass
                );
            manager.ConnectionState += new ConnectionStateEventHandler(ConnectionState);

            try
            {
                manager.Login();
                return true;
            }
            catch
            {
                return false;
            }
        }

private void ConnectionState(object sender, ConnectionStateEvent e)
        {
            if (manager.IsConnected())
            {
                
                MessageBox.Show("on");
                notifyIcon1.Icon = Properties.Resources.phone_on;
            }
            else
            {
                
                MessageBox.Show("off");
                notifyIcon1.Icon = Properties.Resources.phone_off;
            }
        }

Asterisk is in the local net. When I break the connection (pull out the cable), manager.IsConnected() is true, but must be false. I don't understand this behavior. How I can check connection?

1

There are 1 answers

0
Dmitriy G On

I seem to have found a particular solution for my case. The fact is that it seems that when the ConnectionStateEvent event fires, the state of the ConnectionManager does not change immediately. Why this happens I did not understand. But when adding a delay, the IsConnected method began to work correctly.

private async void Connection_State(object sender, ConnectionStateEvent e)
        {
            await Task.Delay(1000);
            MessageBox.Show(manager.IsConnected().ToString());
            if (manager.IsConnected())
            {
                notifyIcon1.Icon = Properties.Resources.phone_on;
            }
            else
            {
                notifyIcon1.Icon = Properties.Resources.phone_off;
            }
        }