//
        // PUBLIC COM INTERFACE IFocuserV3 IMPLEMENTATION
        //

        #region Common properties and methods.

        /// <summary>
        /// Displays the Setup Dialog form.
        /// If the user clicks the OK button to dismiss the form, then
        /// the new settings are saved, otherwise the old values are reloaded.
        /// THIS IS THE ONLY PLACE WHERE SHOWING USER INTERFACE IS ALLOWED!
        /// </summary>
        public void SetupDialog()
        {
            if (Server.FocusersInUse > 1)
            {
                System.Windows.MessageBox.Show("Unable to change Focuser Properties at this time.");

                return;
            }

            if (ConnectedState)
            {
                System.Windows.MessageBox.Show("Already connected, just press OK");

                return;
            }

            // Launch the setup dialog on the U/I thread.

            Task taskShow = new Task(() =>
            {
                FocuserDriverSetupDialogViewModel vm = new FocuserDriverSetupDialogViewModel();
                FocuserDriverSetupDialogView view    = new FocuserDriverSetupDialogView {
                    DataContext = vm
                };
                vm.RequestClose += view.OnRequestClose;
                vm.InitializeCurrentFocuser(FocuserManager.FocuserID);
                vm.IsLoggingEnabled   = _logger.Enabled;
                view.ContentRendered += (sender, eventArgs) => view.Activate();

                bool?result = view.ShowDialog();

                if (result.HasValue && result.Value)
                {
                    _logger.Enabled          = vm.IsLoggingEnabled;
                    string focuserID         = vm.FocuserSetupVm.FocuserID;
                    FocuserManager.FocuserID = focuserID;
                    SaveProfile();
                    AppSettingsManager.SaveAppSettings();

                    view.DataContext = null;
                    view             = null;

                    vm.Dispose();
                    vm = null;
                }
            });

            taskShow.Start(Globals.UISyncContext);

            // Wait for the task to be completed and the profile to be updated.

            taskShow.Wait();
        }
 private void SaveApplicationSettings()
 {
     Globals.SuppressTrayBubble = SuppressTrayBubble;
     Globals.UseCustomTheme     = UseCustomTheme;
     AppSettingsManager.SaveAppSettings();
 }
        internal static void Startup(string[] args)
        {
            // Uncomment the following lines to allow the Visual Studio Debugger to be
            // attached to the server for debugging.

            //int procId = Process.GetCurrentProcess().Id;
            //string msg = String.Format( "Attach the debugger to process #{0} now.", procId );
            //MessageBox.Show( msg );

            Globals.UISyncContext = TaskScheduler.FromCurrentSynchronizationContext();

            LoadComObjectAssemblies();               // Load served COM class assemblies, get types

            if (!ProcessArguments(args))             // Register/Unregister
            {
                App.Current.Shutdown();

                return;
            }

            // Now that we are past the registration code, make sure that we are NOT
            // running As Administrator.

            if (IsElevated && IsUacEnabled)
            {
                MessageBox.Show("You cannot run this application with elevated access...Terminating!");
                App.Current.Shutdown();

                return;
            }

            // Initialize critical member variables.

            _objsInUse     = 0;
            _scopesInUse   = 0;
            _domesInUse    = 0;
            _focusersInUse = 0;
            _serverLocks   = 0;

            // Application Startup

            // Initialize our non-U/I services.

            ServiceInjector.InjectServices();

            // Create Device Hub App Settings and seed with default values.

            AppSettingsManager.CreateInitialAppSettings();

            // Load the saved position of the main window. Do this before the
            // window object is created so that the code-behind can pick up
            // the location.

            AppSettingsManager.LoadMainWindowSettings();

            // Create the View and ViewModel

            ViewModel  = new MainWindowViewModel();
            MainWindow = new MainWindow();

            // Create the U/I services.

            ServiceInjector.InjectUIServices(MainWindow);

            MainWindow.DataContext = ViewModel;
            MainWindow.Closing    += MainWindow_Closing;

            // Load the saved settings to ensure that everyone up-to-date. Be sure to do this
            // after the main window is created so we can set its location.

            AppSettingsManager.LoadAppSettings();

            LoadDeviceSettings();

            // Register the class factories of the served objects

            RegisterClassFactories();

            StartGarbageCollection(10000);                  // Collect garbage every 10 seconds.

            try
            {
                ShowMainWindow();
            }
            finally
            {
                AppSettingsManager.SaveAppSettings();

                // Update the dome settings to save the azimuth adjustment.

                SaveDomeSettings();

                // Revoke the class factories immediately.
                // Don't wait until the thread has stopped before
                // we perform revocation!!!

                RevokeClassFactories();

                MainWindow.DataContext = null;
                MainWindow             = null;
                ViewModel.Dispose();
                ViewModel = null;

                ServiceContainer.Instance.ClearAllServices();

                // Now stop the Garbage Collector task.

                StopGarbageCollection();
            }
        }
 private void SaveOffset()
 {
     Globals.FocuserTemperatureOffset = TemperatureOffset;
     AppSettingsManager.SaveAppSettings();
 }