简体   繁体   中英

event fires multiple times (Lync SDK 2013)

I use the Lync SDK 2013. When creating a new conversation (of any type, not audio/video only) my conversation_added event triggers multiple times.

Having a permanent access to the LyncClient requires creating a timer check every second for a valid connection to the lync application.

I created a snippet that should work in WinForms applications

public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            InitializeConnectionTimer();
        }

        private LyncClient client;
        private ConversationManager conversationManager;
        private Timer connectionTimer;
        private bool networkAvailable;

        private void InitializeConnectionTimer()
        {
            connectionTimer = new Timer
            {
                Interval = 1000
            };

            connectionTimer.Tick += connectionTimer_Tick;

            connectionTimer.Start();
        }

        private void CheckConnection()
        {
            TrySetClient();
            SetConversationManager();
        }

        private void TrySetClient()
        {
            client = null;

            try
            {
                client = LyncClient.GetClient();
                client.ClientDisconnected += Client_Disconnected;
                client.StateChanged += Client_StateChanged;
            }
            catch (Exception)
            {
            }
        }

        private void SetConversationManager()
        {
            if (client != null)
            {
                conversationManager = client.ConversationManager;
                conversationManager.ConversationAdded += Conversation_Added;
            }
            else
            {
                conversationManager = null;
            }
        }

        private void Client_Disconnected(object sender, EventArgs e)
        {
            CheckConnection();
        }

        private void Client_StateChanged(object sender, ClientStateChangedEventArgs e)
        {
            CheckConnection();
        }

        private void connectionTimer_Tick(object sender, EventArgs e)
        {
            CheckConnection();
        }

        private void Conversation_Added(object sender, ConversationManagerEventArgs e)
        {
            System.Diagnostics.Process.Start("https://www.google.com/"); // open Browser window here
        }
    }

and you can see the full example here

https://pastebin.com/1tR3v8We

I think the error appears because I always attach additional event listeners to the LyncClient. But I have to check the client connection on TrySetClient() every second because the Skype application may get closed, crashed etc.

How can I fix this?

This is not a lync-client-sdk problem but a classic C# event problem.

You need to remove your current handles before connecting new handles. You should be doing this before you clear your client pointer.

There is a "trick" that you can do if you don't know if you have connected a handler or not. You can remove a handler and if it doesn't exist it's just ignored.

This allows you to do this:

client = LyncClient.GetClient();
client.ClientDisconnected -= Client_Disconnected;
client.ClientDisconnected += Client_Disconnected;
client.StateChanged -= Client_StateChanged;
client.StateChanged += Client_StateChanged;

If you do this for all your handles then that will fix your problem.

It is more highly recommended that you remove your handles nicely when your finished with them as leaving them connected may keep your classes in memory. This can lead to live leaks if your not careful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM