// // 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 { /// /// Interaction logic for App.xaml /// 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; } } }