Пример #1
0
        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            if (!App.Current.IsShadowProcess)
            {
                // a shadow process is always started by a non-shadow process, which
                // should already have the startup registry value set
                App.Current.SetStartupWithWindows(AppSettings.Default.StartWithWindows);
            }

#if DEBUG && !DEBUG_SINGLE_INSTANCE
            this.DisplayRootViewFor <MainWindowViewModel>();
            return;
#else
            var assemblyLocation = Assembly.GetEntryAssembly().Location;

            var identifier = assemblyLocation.Replace('\\', '_');
            if (!SingleInstance <App> .InitializeAsFirstInstance(identifier))
            {
                Environment.Exit(0);
            }

            if (App.Current.DoNotSpawnShadowExecutable || App.Current.IsShadowProcess)
            {
                this.DisplayRootViewFor <MainWindowViewModel>();
            }
            else
            {
                AppBootstrapper.SpawnShadowProcess(e, assemblyLocation);
                Environment.Exit(0);
            }
#endif
        }
        private static void SpawnShadowProcess(StartupEventArgs e, string assemblyLocation)
        {
            var shadowAssemblyName = $"{Path.GetFileNameWithoutExtension(assemblyLocation)}.shadow.exe";
            var shadowPath         = Path.Combine(Path.GetTempPath(), "FBDashboard", shadowAssemblyName);

            try
            {
                if (File.Exists(shadowPath))
                {
                    File.Delete(shadowPath);
                }

                Debug.Assert(assemblyLocation != null, "assemblyLocation != null");
                Directory.CreateDirectory(Path.GetDirectoryName(shadowPath));
                File.Copy(assemblyLocation, shadowPath);

                // Copy FBuild folder with worker if exists.
                var workerFolder       = Path.Combine(Path.GetDirectoryName(assemblyLocation), "FBuild");
                var workerTargetFolder = Path.Combine(Path.GetDirectoryName(shadowPath), "FBuild");
                if (Directory.Exists(workerFolder))
                {
                    Directory.CreateDirectory(workerTargetFolder);
                    // Copy all worker files.
                    foreach (string newPath in Directory.GetFiles(workerFolder, "*.*", SearchOption.TopDirectoryOnly))
                    {
                        File.Copy(newPath, newPath.Replace(workerFolder, workerTargetFolder), true);
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                // may be already running
            }
            catch (IOException)
            {
                // may be already running
            }

            AppBootstrapper.CreateShadowContext(shadowPath);
            SingleInstance <App> .Cleanup();

            var ShadowWorkingDir = Directory.GetCurrentDirectory();

            if (File.Exists(Path.Combine(Path.GetDirectoryName(shadowPath), "FBuild", "FBuildWorker.exe")))
            {
                ShadowWorkingDir = Path.GetDirectoryName(shadowPath);
            }

            Process.Start(new ProcessStartInfo
            {
                FileName         = shadowPath,
                Arguments        = string.Join(" ", e.Args.Concat(new[] { AppArguments.ShadowProc })),
                WorkingDirectory = ShadowWorkingDir
            });
        }
        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            if (!App.Current.IsShadowProcess)
            {
                // a shadow process is always started by a non-shadow process, which
                // should already have the startup registry value set
                App.Current.SetStartupWithWindows(AppSettings.Default.StartWithWindows);
            }

#if DEBUG && !DEBUG_SINGLE_INSTANCE
            this.DisplayRootViewFor <MainWindowViewModel>();
            return;
#else
            var assemblyLocation = Assembly.GetEntryAssembly().Location;

            var identifier = assemblyLocation.Replace('\\', '_');
            if (!SingleInstance <App> .InitializeAsFirstInstance(identifier))
            {
                Environment.Exit(0);
            }

            if (App.Current.DoNotSpawnShadowExecutable || App.Current.IsShadowProcess)
            {
                if (App.Current.IsShadowProcess)
                {
                    AppBootstrapper.UpdateOriginal(e);
                    var checkTimer = new Timer(1000 * 60 * 15);                 // Check for changes every 15 minutes.
                    checkTimer.Elapsed  += (senderTimer, eTimer) => AppBootstrapper.UpdateOriginal(e);
                    checkTimer.AutoReset = true;
                    checkTimer.Enabled   = true;
                }
                this.DisplayRootViewFor <MainWindowViewModel>();
            }
            else
            {
                System.Threading.Thread.Sleep(5000);                 // Wait few seconds to spawn shadow process. It might be still locked after update.
                AppBootstrapper.SpawnShadowProcess(e, assemblyLocation);
                Environment.Exit(0);
            }
#endif
        }