private void ConnectButton_Click(object sender, RoutedEventArgs args) { try { // Clear any previous connection error ClearConnectionStatusLabel(); bool isSecure = SecureConnectionCheckBox.IsChecked.HasValue && SecureConnectionCheckBox.IsChecked.Value; // Saving user input to disk var persistor = new UserInputPersistor { Host = m_host, Exchange = m_exchange, TopicPattern = TopicTextBox.Text, Username = UsernameTextBox.Text, IsSecureConnection = isSecure }; UserInputPersistor.Save(persistor); // Updating UI state ChangeUiState(canConnect: false, canTerminate: true); try { // Connecting var connRequest = new ConnectionRequest(host: m_host, exc: m_exchange, secure: isSecure, user: UsernameTextBox.Text, pwd: PasswordTextBox.Password, topic: TopicTextBox.Text); m_appLogic.Connect(connRequest); } catch (InvalidOperationException e) { // Showing the error in UI SetConnectionStatusText(text: e.Message, isProblem: true); return; } args.Handled = true; } catch (Exception e) { HandleBug(e); } }
/// <summary> /// Opens a connection. /// </summary> /// <param name="connRequest">Connection request.</param> /// <exception cref="InvalidOperationException">Thrown if object state is invalid.</exception> /// <exception cref="ObjectDisposedException">Thrown if the object has been disposed.</exception> public void Connect(ConnectionRequest connRequest) { ExpectNotDisposed(); // throws ObjectDisposedException // Locking a relatively large amount of code. However, none of the operations // is expected to last long, and a race condition must not occur. lock (m_amqpClientLock) { if (m_amqpClient != null) { throw new InvalidOperationException("Connection already open"); } // Creating the client object. Using a lock to synchronise the variable between threads. m_amqpClient = CreateAmqpClient(connRequest); // Connecting. This is not within a lock, as the connect operation // supposedly can take relatively long or at least cause a deadlock in this class. m_amqpClient.Connect(connRequest); } }