0

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 Answers1

0

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!

Paul Nearney
  • 6,965
  • 2
  • 30
  • 37
  • So I would have to setup the disconnect event and handle it in there? –  Aug 14 '18 at 14:12
  • 1
    Yes. And be warned, the Disconnect event doesn't always fire immediately when the client closes, but you can force it to fire by e.g. reading the state property. So you can use the same timer when the client is running to call _client.State at regular intervals, to ensure the Disconnect event fires in a timely manner – Paul Nearney Aug 14 '18 at 14:17
  • Would you mind providing a small code example? Because when would I have to setup my events? Within the try catch? How would I remove the current event handlers after loosing the current instance? I think some problems might occur –  Aug 14 '18 at 14:18
  • Sorry - I don't have any code to hand, just going from memory having been bitten by this multiple times before! – Paul Nearney Aug 14 '18 at 14:20