//***************************************************************************************** // ConversationManager Event Handling // // ConversationAdded occurs when: // 1) A new conversation was created by this application // 2) A new conversation was created by another third party application or Lync itself // 2) An invite was received at this endpoint (InstantMessaging / AudioVideo) // // ConversationRemoved occurs when: // 1) A conversation is terminated // //***************************************************************************************** /// <summary> /// Called when a new conversation is added (incoming or outgoing). /// /// Will create a window for this new conversation and show it. /// </summary> void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e) { //***************************************************************************************** // Registering for events // // It is very important that registering for an object's events happens within the handler // of that object's added event. In another words, the application should register for the // conversation events within the ConversationAdded event handler. // // This is required to avoid timing issues which would cause the application to miss events. // While this handler method is executing, the Lync client is unable to process events for // this application (synce its thread is running this method), so no events will be lost. // // By registering for events here, we guarantee that all conversation related events will be // caught the first time they occur. // // We want to show the availability of the buttons in the conversation window based // on the ActionAvailability events. The solution below uses a lock to allow the window // to load while holding the event queue. This prevents events from being raised even // before the user interface controls get a change to load. // //***************************************************************************************** //creates a new window (which will register for Conversation and child object events) ConversationWindow window = new ConversationWindow(e.Conversation, client); //posts the execution into the UI thread this.BeginInvoke(new MethodInvoker(delegate() { //adds the window to the dictionary conversationWindows.Add(e.Conversation, window); //registers for window load events so that the lock of the Lync thread can be released window.Load += window_Load; //shows the new window window.Show(this); })); //waits until the window is loaded to release the SDK thread conversationWindowLock.WaitOne(); }
/// <summary> /// Called when a conversation is removed. /// /// Will dispose the window associated with the removed conversation. /// </summary> void ConversationManager_ConversationRemoved(object sender, ConversationManagerEventArgs e) { //posts the execution into the UI thread this.BeginInvoke(new MethodInvoker(delegate() { //checks if a conversation window was created, and dispose it if (conversationWindows.ContainsKey(e.Conversation)) { //gets the existing conversation window ConversationWindow window = conversationWindows[e.Conversation]; //remove the conversation from the dictionary conversationWindows.Remove(e.Conversation); //closes and disposes window.Close(); window.Dispose(); } })); }