C# Lync SDK - LyncClient subscribe to events

155 Views Asked by At

I started creating a WinForms application for the Lync SDK. I want the client subscribing to some events.

public partial class FrmMain : Form
{
        public FrmMain()
        {
            InitializeComponent();
            client = new Client(this);
        }
}



    internal class Client
    {
        public Client(FrmMain frm)
        {
            this.frm = frm;
        }

        private FrmMain frm;

        public LyncClient Instance
        {
            get
            {
                LyncClient client = null;

                try
                {
                    client = LyncClient.GetClient();
                }
                catch (ClientNotFoundException)
                {
                }

                return client;
            }
        }

        private void Client_StateChanged(Object source, ClientStateChangedEventArgs e)
        {
            frm.UpdateSignedInState();
        }
}

Where do I have to subscribe the client.StateChanged event?

client.StateChanged += Client_StateChanged;

When closing my Lync client or disconnecting I would loose the current instance so where would I have to subscribe the event that it will always trigger on state changes?

1

There are 1 best solutions below

4
Paul Nearney On

That's right - the instance is only available while the client is running, so when it's not running there is nothing to wire up to.

A common pattern is to set a timer when the client disconnects, and call GetClient() in the callback. If it succeeds, wire up your events. Otherwise, reset your timer and try again!