CreateAndRun() public static method

public static CreateAndRun ( ) : void
return void
        private static void MainInner(string[] args)
        {
            if (AppUpdater.OnStartup(args))
            {
                return;
            }

            if (args.Length == 2 && args[0] == "--run")
            {
                Process.Start(args[1]);
                return;
            }

            var appGuid = ((GuidAttribute)Assembly.GetEntryAssembly().GetCustomAttributes(typeof(GuidAttribute), true).GetValue(0)).Value;
            var mutexId = $@"Global\{{{appGuid}}}";

            if (Array.IndexOf(args, WindowsHelper.RestartArg) != -1)
            {
                for (var i = 0; i < 999; i++)
                {
                    Thread.Sleep(200);

                    using (var mutex = new Mutex(false, mutexId)) {
                        if (mutex.WaitOne(0, false))
                        {
                            break;
                        }
                    }
                }

                ProcessExtension.Start(MainExecutingFile.Location, args.Where(x => x != WindowsHelper.RestartArg));
                return;
            }

            using (var mutex = new Mutex(false, mutexId)) {
                _secondInstanceMessage = User32.RegisterWindowMessage(mutexId);
                if (mutex.WaitOne(0, false))
                {
                    if (AppUpdater.OnUniqueStartup(args))
                    {
                        return;
                    }

                    _initialized = true;
                    App.CreateAndRun(false);

                    /*if (args.Length == 0) {
                     *  TryToRunAppSafely();
                     * } else {
                     *  App.CreateAndRun(false);
                     * }*/
                }
                else
                {
                    PassArgsToRunningInstance(args);
                }
            }
        }
Ejemplo n.º 2
0
        public static void MainInner(string[] args)
        {
            if (AppUpdater.OnStartup(args))
            {
                return;
            }

            var appGuid = ((GuidAttribute)Assembly.GetEntryAssembly().GetCustomAttributes(typeof(GuidAttribute), true).GetValue(0)).Value;
            var mutexId = $@"Global\{{{appGuid}}}";

            if (args.Contains(WindowsHelper.RestartArg))
            {
                for (var i = 0; i < 999; i++)
                {
                    Thread.Sleep(200);

                    using (var mutex = new Mutex(false, mutexId)) {
                        if (mutex.WaitOne(0, false))
                        {
                            break;
                        }
                    }
                }

                ProcessExtension.Start(MainExecutingFile.Location, args.Where(x => x != WindowsHelper.RestartArg));
                return;
            }

            using (var mutex = new Mutex(false, mutexId)) {
                SecondInstanceMessage = User32.RegisterWindowMessage(mutexId);
                if (mutex.WaitOne(0, false))
                {
                    _initialized = true;
                    App.CreateAndRun();
                }
                else
                {
                    PassArgsToRunningInstance(args);
                }
            }
        }
Ejemplo n.º 3
0
        private static void TryToRunAppSafely()
        {
            var tryingToRunFlag = Path.Combine(ApplicationDataDirectory, "Trying to run.flag");

            FileUtils.EnsureFileDirectoryExists(tryingToRunFlag);

            var failedLastTime = File.Exists(tryingToRunFlag);

            if (failedLastTime)
            {
                FileUtils.TryToDelete(tryingToRunFlag);
                App.CreateAndRun(true);
                return;
            }

            var createdOnce = false;

            DpiAwareWindow.NewWindowCreated += OnWindowCreated;
            App.CreateAndRun(false);

            void OnWindowCreated(object sender, EventArgs args)
            {
                if (App.IsSoftwareRenderingModeEnabled())
                {
                    DpiAwareWindow.NewWindowCreated -= OnWindowCreated;
                    return;
                }

                var window = (DpiAwareWindow)sender;

                if (TryToCreate())
                {
                    if (window.Content == null)
                    {
                        window.Content = new Border();
                    }

                    window.ContentRendered += OnWindowRendered;
                }
            }

            async void OnWindowRendered(object sender, EventArgs args)
            {
                var window = (DpiAwareWindow)sender;

                window.ContentRendered -= OnWindowRendered;

                await Task.Yield();

                if (!_crashed)
                {
                    FileUtils.TryToDelete(tryingToRunFlag);
                }
            }

            bool TryToCreate()
            {
                if (createdOnce || File.Exists(tryingToRunFlag))
                {
                    return(false);
                }
                createdOnce = true;
                File.WriteAllBytes(tryingToRunFlag, new byte[0]);
                DpiAwareWindow.NewWindowCreated -= OnWindowCreated;
                return(true);
            }
        }