Example #1
0
        /// <summary>
        /// Initializes the application's name and sets up a handler to report uncaught
        /// exceptions.
        /// </summary>
        /// <param name="applicationName">The name of the application to pass to <see cref="ApplicationInfo.Initialize"/>.</param>
        /// <param name="showException">The action to call when an exception needs to be shown.  This can be null,
        /// which will cause <see cref="ShowError"/> to be called.</param>
        /// <param name="applicationAssembly">The assembly that's initializing the application, typically the main executable.</param>
        public static void InitializeApplication(string applicationName, Action <Exception>?showException, Assembly?applicationAssembly = null)
        {
            ApplicationInfo.Initialize(applicationName, applicationAssembly ?? Assembly.GetCallingAssembly(), () => HandleUtility.IsApplicationActivated);

            Application.ThreadException += (sender, e) =>
            {
                Exception ex = e.Exception;
                Log.Error(typeof(WindowsUtility), "An unhandled exception occurred in a Windows Forms thread.", ex);
                ApplicationInfo.ShowUnhandledException(ex, message => ShowError(null, message), showException);
            };

            // Setup the current thread and any other Windows Forms threads to route unhandled
            // exceptions to the Application.ThreadException handler.
            try
            {
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic, true);
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic, false);
            }
#pragma warning disable CC0004 // Catch block cannot be empty
            catch (InvalidOperationException)
            {
                // If the process is running in the Visual Studio hosting process (e.g., *.vshost.exe),
                // then on subsequent runs the mode can't be set again.
            }
#pragma warning restore CC0004 // Catch block cannot be empty

            // Set some style settings that all Windows Forms apps should use now.
            SetHighDpiMode();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
        }
Example #2
0
        /// <summary>
        /// Initializes the application's name and sets up a handler to report uncaught
        /// exceptions.
        /// </summary>
        /// <param name="applicationName">The name of the application to pass to <see cref="ApplicationInfo.Initialize"/>.</param>
        /// <param name="showException">The action to call when an exception needs to be shown.  This can be null,
        /// which will cause <see cref="ShowError(Window,string)"/> to be called.</param>
        /// <param name="applicationAssembly">The assembly that's initializing the application, typically the main executable.</param>
        public static void InitializeApplication(string applicationName, Action <Exception>?showException, Assembly?applicationAssembly = null)
        {
            ApplicationInfo.Initialize(applicationName, applicationAssembly ?? Assembly.GetCallingAssembly(), () => HandleUtility.IsApplicationActivated);

            Application.Current.DispatcherUnhandledException += (sender, e) =>
            {
                e.Handled = true;
                Exception ex = e.Exception;
                Log.Error(typeof(WindowsUtility), "An unhandled exception occurred in a Windows thread.", ex);
                ApplicationInfo.ShowUnhandledException(ex, message => e.Dispatcher.BeginInvoke(new Action(() => ShowError(null, message))), showException);
            };
        }
Example #3
0
        public void InitializeApplicationNameTest()
        {
            // Before Initialize is called, the app name gets pulled from AppDomain.FriendlyName
            string applicationName = ApplicationInfo.ApplicationName;

            applicationName.ShouldNotBeNull();

            applicationName = "Testing";
            bool isActivated = true;

            ApplicationInfo.Initialize(applicationName, isActivated: () => isActivated);
            ApplicationInfo.ApplicationName.ShouldBe(applicationName);

            ApplicationInfo.IsActivated.ShouldBe(isActivated);
            isActivated = false;
            ApplicationInfo.IsActivated.ShouldBe(isActivated);
        }
Example #4
0
        private static void Main(string[] args)
        {
            var appInfo = ApplicationInfo.Initialize(null);

            Console.WriteLine($"AppTitle: {appInfo.Title}");
            Console.WriteLine($"Version:{appInfo.InformationalVersion} BuildTime:{appInfo.BuildTimestampLocal}; Framework:{appInfo.FrameworkName}");

            var asmAbstracts = new AssemblyInfo(typeof(AssemblyInfo).Assembly);

            Console.WriteLine($"AssemblyTitle: {asmAbstracts.Title}");
            Console.WriteLine($"Version:{asmAbstracts.InformationalVersion} BuildTime:{asmAbstracts.BuildTimestampLocal}; Framework:{asmAbstracts.FrameworkName}");

            var asmConfig = new AssemblyInfo(typeof(DJson).Assembly);

            Console.WriteLine($"AssemblyTitle: {asmConfig.Title}");
            Console.WriteLine($"Version:{asmConfig.InformationalVersion} BuildTime:{asmConfig.BuildTimestampLocal}; Framework:{asmConfig.FrameworkName}");
        }
Example #5
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            LogMessage(string.Format("Entering {0}.Initialize()", this.ToString()));
            try
            {
                await base.InitializeAsync(cancellationToken, progress).ConfigureAwait(true);

                LogMessage(string.Format("After {0}'s base.Initialize()", this.ToString()));

                // Set the ApplicationInfo's main assembly so IsDebugBuild will work correctly.
                ApplicationInfo.Initialize(Title, this.GetType().Assembly);

                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                ScanInfo.GetUserRegistryRoot = () => this.UserRegistryRoot;

                // Ryan Molden from Microsoft says that GetDialogPage caches the result, so we're going to cache it too.
                // I've also verified GetDialogPage's caching implementation in VS11 by looking at it with Reflector.
                // http://social.msdn.microsoft.com/Forums/eu/vsx/thread/303fce01-dfc0-43b3-a578-8b3258c0b83f
                // From http://msdn.microsoft.com/en-us/library/bb165039.aspx
                baseConverterOptions = this.GetDialogPage(typeof(BaseConverter.Options)) as BaseConverter.Options;
                generalOptions       = this.GetDialogPage(typeof(Options)) as Options;
                highlightOptions     = this.GetDialogPage(typeof(HighlightOptions)) as HighlightOptions;
                projectOptions       = this.GetDialogPage(typeof(Projects.Options)) as Projects.Options;
                regionOptions        = this.GetDialogPage(typeof(Regions.Options)) as Regions.Options;
                sortOptions          = this.GetDialogPage(typeof(Sort.Options)) as Sort.Options;
                taskOptions          = this.GetDialogPage(typeof(Tasks.Options)) as Tasks.Options;

                this.processor = new CommandProcessor(this);

                // Add our command handlers.  Commands must exist in the .vsct file.
                if (await this.GetServiceAsync(typeof(IMenuCommandService)).ConfigureAwait(true) is OleMenuCommandService mcs)
                {
                    foreach (Command id in Enum.GetValues(typeof(Command)))
                    {
                        CommandID commandId = new(Guids.MeneesVsToolsCommandSet, (int)id);

                        // OleMenuCommand extends the base MenuCommand to add BeforeQueryStatus.
                        // http://msdn.microsoft.com/en-us/library/bb165468.aspx
                        OleMenuCommand menuItem = new(this.Command_Execute, commandId);
                        menuItem.BeforeQueryStatus += this.Command_QueryStatus;
                        mcs.AddCommand(menuItem);
                    }
                }

                // To make our font and color formats customizable in non-TextEditor windows,
                // (e.g., Output) we have to hook some old-school TextManager COM events.
                this.formatManager = new ClassificationFormatManager(this.ServiceProvider);
                this.formatManager.UpdateFormats();

                // This option requires a restart if changed because the CommentTaskProvider and various
                // XxxMonitor classes attach to too many events and register too many things to easily
                // detach/unregister and clean them all up if this is toggled interactively.
                if (TaskOptions.EnableCommentScans)
                {
                    this.commentTaskProvider = new CommentTaskProvider(this);
                }

                this.buildTimer = new BuildTimer(this);
            }
            catch (Exception ex)
            {
                LogMessage(string.Format("An unhandled exception occurred in {0}.Initialize().", this.ToString()), ex);
                throw;
            }

            LogMessage(string.Format("Exiting {0}.Initialize()", this.ToString()));
        }