IConnectionStateProvider.ConnectionStateChanged Event

Occurs when connection status of the IConnectionStateProvider has been changed.

Namespace:  TwinCAT
Assembly:  TwinCAT.Ads.Abstractions (in TwinCAT.Ads.Abstractions.dll) Version: 6.0.328+39e3229

Syntax

C#

event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged

Value

Type: System.EventHandler.ConnectionStateChangedEventArgs.

Remarks

The Connection state changes only if the IConnection is established / shut down or active communication is triggered by the User of the IConnection object.

Examples

The following sample shows how to keep the ConnectionState updated by triggering ADS Communication.

Trigger ConnectionState changes in WPF Applications

private DispatcherTimer _timer = null;
private AdsSession _session = null;
//private AdsConnection _connection = null;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    _session = new AdsSession(AmsNetId.Local, 10000);
    IConnection connection = _session.Connect();
    tbConnectionState.Text = connection.ConnectionState.ToString();
    _session.ConnectionStateChanged += _session_ConnectionStateChanged;

    _timer = new DispatcherTimer();
    _timer.Interval = TimeSpan.FromMilliseconds(200);
    _timer.Tick += TimerOnTick;

    _timer.Start();
}

private void Window_Unloaded(object sender, RoutedEventArgs e)
{
    _timer.Stop();
    _session.Dispose();
}

private void _session_ConnectionStateChanged(object sender, TwinCAT.ConnectionStateChangedEventArgs e)
{
    // ConnectionStateChanged will be triggered by communication Invokes
    tbConnectionState.Text = e.NewState.ToString();
}

private void TimerOnTick(object sender, EventArgs eventArgs)
{
    // The Timer Event will occur here in the UI thread because its an DispatcherTimer event!
    // An active ADS request will trigger Connection State periodically!
    StateInfo stateInfo;
    if (_session.Connection.TryReadState(out stateInfo) == AdsErrorCode.NoError)
    {
    tbAdsState.Text = stateInfo.AdsState.ToString();
    }
    else
    {
    tbAdsState.Text = "Invalid";
    }
}

Reference

IConnectionStateProvider Interface

TwinCAT Namespace

IConnectionStateProvider.ConnectionState