/// <summary> /// Gets the Lync client, initializes if in UI suppression, and /// starts the user sign in process. This method can raise exceptions /// which are thrown if the calling form has not registered a callback for /// exception specific events that are declared in this class. /// </summary> /// <param name="sideBySide">boolean. Specifies endpoint mode</param> internal void StartUpLync(Boolean sideBySide) { //Calling GetClient a second time in a running process will //return the previously cached client. For example, calling GetClient(boolean sideBySideFlag) // the first time in a process returns a new endpoint. Calling the method a second //time returns the original endpoint. If you call GetClient(false) to get a client //endpoint and then GetClient(true), the original client enpoint is returned even though // a true value argument is passed with the second call. try { if (_LyncClient == null) { //If sideBySide == false, a standard endpoint is created //Otherwise, a side-by-side endpoint is created _LyncClient = LyncClient.GetClient(sideBySide); } _inSideBySideMode = sideBySide; //Display the current state of the Lync client. if (ClientStateChanged != null) { ClientStateChanged(_LyncClient.State.ToString()); } //Register for the three Lync client events needed so that application is notified when: // * Lync client signs in or out _LyncClient.StateChanged += _LyncClient_StateChanged; _LyncClient.SignInDelayed += _LyncClient_SignInDelayed; _LyncClient.CredentialRequested += _LyncClient_CredentialRequested; //Client state of uninitialized means that Lync is configured for UI suppression mode and //must be initialized before a user can sign in to Lync if (_LyncClient.State == ClientState.Uninitialized) { _LyncClient.BeginInitialize( (ar) => { _LyncClient.EndInitialize(ar); _thisProcessInitializedLync = true; }, null); } else if (_LyncClient.State == ClientState.SignedIn) { if (UserIsSignedIn != null) { UserIsSignedIn(_LyncClient); } } //If the Lync client is signed out, sign into the Lync client else if (_LyncClient.State == ClientState.SignedOut) { SignUserIn(); } else if (_LyncClient.State == ClientState.SigningIn) { if (MessageBox.Show( "Lync is signing in. Do you want to continue waiting?", "Sign in delay", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) { if (CloseAppConditionHit != null) { CloseAppConditionHit(); } } } } catch (NotInitializedException ni) { MessageBox.Show( "Client is not initialized. Closing form", "Lync Client Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); //If calling form has registered a handler for this delegate, //call the delegate if (CloseAppConditionHit != null) { CloseAppConditionHit(); } //otherwise, throw the exception. else { throw ni; } } catch (ClientNotFoundException cnf) { MessageBox.Show( "Client is not running. Closing form", "Lync Client Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); //If calling form has registered a handler for this delegate, //call the delegate if (CloseAppConditionHit != null) { CloseAppConditionHit(); } //otherwise, throw the exception. else { throw cnf; } } catch (Exception exc) { MessageBox.Show( "General exception: " + exc.Message, "Lync Client Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); //If calling form has registered a handler for this delegate, //call the delegate if (CloseAppConditionHit != null) { CloseAppConditionHit(); } //otherwise, throw the exception. else { throw exc; } } }
/// <summary> /// Gets the Lync client, initializes if in UI suppression, and /// starts the user sign in process /// </summary> /// <param name="sideBySide">boolean. Specifies endpoint mode</param> internal void StartUpLync(Boolean sideBySide) { //Calling GetClient a second time in a running process will //return the previously cached client. For example, calling GetClient(boolean sideBySideFlag) // the first time in a process returns a new endpoint. Calling the method a second //time returns the original endpoint. If you call GetClient(false) to get a client //endpoint and then GetClient(true), the original client enpoint is returned even though // a true value argument is passed with the second call. try { if (_LyncClient == null) { //If sideBySide == false, a standard endpoint is created //Otherwise, a side-by-side endpoint is created _LyncClient = LyncClient.GetClient(sideBySide); } //if (_LyncClient.Self.Contact == null) //{ // foreach (var process in System.Diagnostics.Process.GetProcessesByName(Constants.PROCESSNAME)) // { // process.Kill(); // } // var unused = _LyncClient.State; // _LyncClient = LyncClient.GetClient(); //} if (_LyncClient.State == ClientState.Invalid) { _LyncClient = LyncClient.GetClient(false); } _inSideBySideMode = sideBySide; //Display the current state of the Lync client. if (ClientStateChanged != null) { ClientStateChanged(_LyncClient.State.ToString()); } //Register for the three Lync client events needed so that application is notified when: // * Lync client signs in or out _LyncClient.StateChanged += _LyncClient_StateChanged; _LyncClient.SignInDelayed += _LyncClient_SignInDelayed; _LyncClient.CredentialRequested += _LyncClient_CredentialRequested; //Client state of uninitialized means that Lync is configured for UI suppression mode and //must be initialized before a user can sign in to Lync if (_LyncClient.State == ClientState.Uninitialized) { _LyncClient.BeginInitialize( (ar) => { _LyncClient.EndInitialize(ar); _thisProcessInitializedLync = true; }, null); } //If the Lync client is signed out, sign into the Lync client if (_LyncClient.State == ClientState.SignedOut) { SignUserIn(); } // don't wait more than 5 seconds. int waitCounter = 0; while (_LyncClient.State == ClientState.SigningIn && waitCounter < 50) { //if (MessageBox.Show( // "Lync is signing in. Do you want to continue waiting?", // "Sign in delay", // MessageBoxButtons.YesNo, // MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No) //{ // if (CloseAppConditionHit != null) // { // CloseAppConditionHit(); // } //} System.Threading.Thread.Sleep(100); waitCounter++; } } catch (NotInitializedException) { MessageBox.Show( "Client is not initialized. Closing form", "Lync Client Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); if (CloseAppConditionHit != null) { CloseAppConditionHit(); } } catch (ClientNotFoundException) { MessageBox.Show( "Client is not running. Closing form", "Lync Client Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); if (CloseAppConditionHit != null) { CloseAppConditionHit(); } } catch (Exception exc) { MessageBox.Show( "General exception: " + exc.Message, "Lync Client Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); if (CloseAppConditionHit != null) { CloseAppConditionHit(); } } }