private async void InstallWindowsServiceOnClick()

in ScpDriverInstaller/MainWindow.xaml.cs [479:570]


        private async void InstallWindowsServiceOnClick(object sender, RoutedEventArgs e)
        {
            MainBusyIndicator.IsBusy = !MainBusyIndicator.IsBusy;
            var failed = false;
            var rebootRequired = false;

            await Task.Run(() =>
            {
                try
                {
                    MainBusyIndicator.SetContentThreadSafe(Properties.Resources.ServiceSetupInstalling);

                    IDictionary state = new Hashtable();
                    var service =
                        new AssemblyInstaller(Path.Combine(GlobalConfiguration.AppDirectory, "ScpService.exe"), null);

                    state.Clear();
                    service.UseNewContext = true;

                    service.Install(state);
                    service.Commit(state);

                    if (StartService(Settings.Default.ScpServiceName))
                    {
                        Log.InfoFormat("{0} started", Settings.Default.ScpServiceName);
                    }
                    else
                    {
                        rebootRequired = true;
                    }
                }
                catch (Win32Exception w32Ex)
                {
                    switch (w32Ex.NativeErrorCode)
                    {
                        case 1073: // ERROR_SERVICE_EXISTS
                            Log.Info("Service already exists");
                            break;

                        default:
                            Log.ErrorFormat("Win32-Error during installation: {0}", w32Ex);
                            failed = true;
                            break;
                    }
                }
                catch (InvalidOperationException iopex)
                {
                    Log.ErrorFormat("Error during installation: {0}", iopex.Message);
                    failed = true;
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Error during installation: {0}", ex);
                    failed = true;
                }
            });

            MainBusyIndicator.IsBusy = !MainBusyIndicator.IsBusy;

            // display error message
            if (failed)
            {
                ExtendedMessageBox.Show(this,
                    Properties.Resources.SetupFailedTitle,
                    Properties.Resources.SetupFailedInstructions,
                    Properties.Resources.SetupFailedContent,
                    string.Format(Properties.Resources.SetupFailedVerbose,
                        new Win32Exception(Marshal.GetLastWin32Error()), Marshal.GetLastWin32Error()),
                    Properties.Resources.SetupFailedFooter,
                    TaskDialogIcon.Error);
                return;
            }

            // display success message
            ExtendedMessageBox.Show(this,
                Properties.Resources.SetupSuccessTitle,
                Properties.Resources.ServiceSetupSuccessInstruction,
                Properties.Resources.ServiceSetupSuccessContent,
                string.Empty,
                string.Empty,
                TaskDialogIcon.Information);

            // display reboot required message
            if (rebootRequired)
            {
                MessageBox.Show(this,
                    Properties.Resources.RebootRequiredContent,
                    Properties.Resources.RebootRequiredTitle,
                    MessageBoxButton.OK,
                    MessageBoxImage.Warning);
            }
        }