private async Task Connect_Worker() { try { MainGrid.IsEnabled = false; await Task.Run(() => { machine.Connect(ComputerName, auth.User, auth.SecurePassword, machine.IncludeInactiveLicenses); }); DialogResult = true; rc = auth.ConfirmCredentials(true); } catch (UnauthorizedAccessException ex) { parent.IsProgressBarRunning = false; rc = auth.ConfirmCredentials(false); if (showUI) { parent.LabelStatus.Text = "Access denied"; MessageBox.Show(ex.Message, "Access denied", MessageBoxButton.OK, MessageBoxImage.Error); parent.LabelStatus.Text = "Getting Computer Name"; } else { showUI = true; } MainGrid.IsEnabled = true; } catch (COMException ex) { parent.IsProgressBarRunning = false; parent.LabelStatus.Text = "DCOM Error"; switch ((uint)ex.ErrorCode) { case 0x80070776: MessageBox.Show(ex.Message, "Microsoft Is Lame", MessageBoxButton.OK, MessageBoxImage.Error); break; default: MessageBox.Show(ex.Message, "DCOM Error", MessageBoxButton.OK, MessageBoxImage.Error); break; } parent.LabelStatus.Text = "Getting Computer Name"; MainGrid.IsEnabled = true; } catch (Exception ex) { parent.IsProgressBarRunning = false; parent.LabelStatus.Text = "Error"; MessageBox.Show("The following error occured: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); parent.LabelStatus.Text = "Getting Computer Name"; MainGrid.IsEnabled = true; } }
public async void button_Connect_Click(object sender, RoutedEventArgs e) { if (ComputerName == "") { MessageBox.Show("Try entering something in the field Computername!", "Noob Alert!!!", MessageBoxButton.OK, MessageBoxImage.Error); return; } parent.LabelStatus.Text = "Connecting..."; MainGrid.IsEnabled = false; if (ComputerName != ".") { auth.ServerName = ComputerName; if (!showUI) { showUI = CheckBoxShowUi.IsChecked.Value; } MainGrid.IsEnabled = false; rc = auth.PromptForPassword(showUI, "Enter username and password of an account with administrative privileges on " + ComputerName, "Authentication Required"); parent.IsProgressBarRunning = true; if (rc == NativeMethods.AuthPrompt.CredUiReturnCodes.ERROR_CANCELLED) { DialogResult = false; return; } } await Connect_Worker(); }