private void SearchAndConnect() { try { clientAddress = (IPAddress)ipAddressesComboBox.SelectedItem; subnetMask = Client.GetSubnetMask(clientAddress); if (clientAddress != null && subnetMask != null) { int port = int.Parse(portTxtBox.Text); Task.Run(() => { return(client.SearchAndConnect(clientAddress, subnetMask, port)); }).ContinueWith((Task <bool> task) => { Dispatcher.Invoke(() => { if (task.Result) { searchBtn.Content = "Disconnect"; IsConnected = true; } }); }); } } catch (Exception exception) { MessageBox.Show(exception.ToString()); } }