App.xaml.cs #1

  • //
  • guest/
  • perforce_software/
  • piper/
  • main/
  • windows/
  • R1.1/
  • Perforce/
  • View/
  • App.xaml.cs
  • View
  • Commits
  • Open Download .zip Download (4 KB)
//
// Copyright 2015 Perforce Software Inc.
//

using Perforce.Helper;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Windows;

namespace Perforce {
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application {

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        public bool DoHandle { get; set; }

        private P4Listener _p4listener;

        private void Application_Startup(object sender, StartupEventArgs e) {
            
            // check to see if the application is already running... if it is, then just set the focus to the existing application and exit
            var proc = PriorProcess();
            if (proc != null && !Perforce.Properties.Settings.Default.IsRestarting) {
                SetForegroundWindow(proc.MainWindowHandle);
                var args = Environment.GetCommandLineArgs();
                if (args.Length > 1) {
                    var path = args[1];
                    if (path.StartsWith("p4://")) {
                        ShowPath(proc, path);
                    }
                }
                Utility.ShutdownApplication(0);
            }

            Perforce.Properties.Settings.Default.IsRestarting = false;
            Log.Info("--- application startup");
            string version = System.Reflection.Assembly.GetExecutingAssembly()
                                   .GetName().Version.ToString();
            Log.Info(string.Format("--- version: {0}", version));

            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += currentDomain_UnhandledException;
            App.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
            try
            {
                InitializeHelpers();
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
            }
            MainWindow m = null;
            try {
                m = new MainWindow();
                m.Visibility = Visibility.Visible;
                _p4listener = new P4Listener();
            } catch (Exception ex) {
                if (Perforce.Properties.Settings.Default.IsRestarting)
                    Log.Debug("Application_startup restarting");
                var msg = string.Format("Error caught: {0}", ex.Message);
                Log.Error(msg);
                Log.Error(ex.StackTrace);
                // MessageBox.Show(msg);
            }
        }

        void InitializeHelpers() {
            Utility.SetupFavoritesHelper();
            Utility.SetupFileMappings();
            Utility.SetupPerforceHelper();
        }

        void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
            _p4listener.Stop();
            UIHelper.CriticalError(e.Exception);
        }

        void currentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
            var ex = e.ExceptionObject as Exception;
            Log.Exception(ex);
            _p4listener.Stop();
            UIHelper.CriticalError(ex);
        }

        private static void ShowPath(Process process, string path) {            
            using (NamedPipeClientStream pipeStream = new NamedPipeClientStream("PipeTo" + process.Id.ToString())) {
                pipeStream.Connect(1000);
                using (StreamWriter sw = new StreamWriter(pipeStream)) {
                    sw.AutoFlush = true;
                    sw.WriteLine(path);
                }
            }
        }

        private static Process PriorProcess() {
            Process curr = Process.GetCurrentProcess();
            Process[] procs = Process.GetProcessesByName(curr.ProcessName);
            foreach (Process p in procs) {
                if ((p.Id != curr.Id) &&
                    (p.MainModule.FileName == curr.MainModule.FileName))
                    return p;
            }
            return null;
        }
    }
}
# Change User Description Committed
#1 16507 perforce_software Move to main branch.
//guest/perforce_software/piper/windows/R1.1/Perforce/View/App.xaml.cs
#3 16474 Robert Cowham Bring up-to-date with //guest/perforce_software/piper/windows/main/...@16473
#2 13572 alan_petersen updating R1.1
#1 11256 alan_petersen Populate //guest/perforce_software/piper/windows/R1.1/...
from //guest/perforce_software/piper/windows/main/....
//guest/perforce_software/piper/windows/main/Perforce/View/App.xaml.cs
#1 11255 alan_petersen Rename/move file(s)
//guest/perforce_software/piper/windows/Perforce/View/App.xaml.cs
#2 10794 alan_petersen Various updates....
       - made main exception caught (happens if application cannot initialize, for example) more explicit in the message box (was "test" :)
       - CTRL-F now moves focus to search box
       - fixed double-click warning message when item is already locked
       - better thumbnail handling
       - general cleanup
#1 10761 alan_petersen initial drop of Piper for Windows....

this version still has _many_ bugs (er... i mean "unintended features") but I will be updating it over the next week as more stability is added.