public static void Main(string[] args)
        {
            // Look and see if this is the only running instance of the client.
            var procCount =
            #if !DEBUG
                Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
                    .Count(p => p.MainModule.FileName == Process.GetCurrentProcess().MainModule.FileName);
            #else
                Process.GetProcesses().Count(p => p.ProcessName.Contains("ParkitectNexus"));
            #endif

            if (procCount == 1)
            {
                // No matter if the application crashes, we must release the mutex when the app closes. Wrap the app
                // logic in a try-finally block.
            #if !DEBUG
                try
                {
            #endif
                    // Initialize the structure map container.
                    var registry = ObjectFactory.ConfigureStructureMap();
                    registry.IncludeRegistry(new PresenterRegistry());
                    registry.For<IApp>().Singleton().Use<App>();
                    ObjectFactory.SetUpContainer(registry);

                    // Create the form and run its message loop. If arguments were specified, process them within the
                    // form.
                    var presenterFactory = ObjectFactory.GetInstance<IPresenterFactory>();
                    var app = presenterFactory.InstantiatePresenter<App>();
                    if (!app.Initialize(ToolkitType.Wpf))
                        return;

                    ParkitectNexusProtocol.Install(ObjectFactory.GetInstance<ILogger>());

                    if (args.Any())
                    {
                        var options = new AppCommandLineOptions();
                        Parser.Default.ParseArguments(args, options);

                        if (options.Url != null)
                        {
                            NexusUrl url;
                            if (NexusUrl.TryParse(options.Url, out url))
                                app.HandleUrl(url);
                        }
                    }

                    app.Run();
            #if !DEBUG
                }
                catch (Exception e)
                {
                    // Report crash to the server.
                    var crashReporterFactory = ObjectFactory.GetInstance<ICrashReporterFactory>();
                    crashReporterFactory.Report("global", e);

                    // Write the error to the log file.
                    var log = ObjectFactory.GetInstance<ILogger>();
                    log?.WriteLine("Application crashed!", LogLevel.Fatal);
                    log?.WriteException(e);
                }
            #endif
                return;
            }

            // If any arguments are set, pass these on to our main application instance.
            if (args.Any())
            {
                var attempts = 0;
                do
                {
                    try
                    {
                        // Write the specified arguments to a temporary ipc.dat file.
                        using (var fileStream = File.OpenWrite(Path.Combine(AppData.Path, "ipc.dat")))
                        using (var streamWriter = new StreamWriter(fileStream))
                        {
                            var options = new AppCommandLineOptions();
                            Parser.Default.ParseArguments(args, options);

                            if (options.Url != null)
                                streamWriter.WriteLine(options.Url);
                        }

                        return;
                    }
                    catch (IOException)
                    {
                        // If storing the arguments fails, we're in trouble. Let's try it again in a few.
                        Thread.Sleep(500);
                        attempts++;
                    }
                } while (attempts < 5); // Limit to 5 attempts.
            }
        }
Example #2
0
        public static void Main(string[] args)
        {
            // Look and see if this is the only running instance of the client.
            var procCount =
#if !DEBUG
                Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
                .Count(p => p.MainModule.FileName == Process.GetCurrentProcess().MainModule.FileName);
#else
                Process.GetProcesses().Count(p => p.ProcessName.Contains("ParkitectNexus"));
#endif

            if (procCount == 1)
            {
                // No matter if the application crashes, we must release the mutex when the app closes. Wrap the app
                // logic in a try-finally block.
#if !DEBUG
                try
                {
#endif
                // Initialize the structure map container.
                var registry = ObjectFactory.ConfigureStructureMap();
                registry.IncludeRegistry(new PresenterRegistry());
                registry.For <IApp>().Singleton().Use <App>();
                ObjectFactory.SetUpContainer(registry);


                // Create the form and run its message loop. If arguments were specified, process them within the
                // form.
                var presenterFactory = ObjectFactory.GetInstance <IPresenterFactory>();
                var app = presenterFactory.InstantiatePresenter <App>();
                if (!app.Initialize(ToolkitType.Wpf))
                {
                    return;
                }

                ParkitectNexusProtocol.Install(ObjectFactory.GetInstance <ILogger>());

                if (args.Any())
                {
                    var options = new AppCommandLineOptions();
                    Parser.Default.ParseArguments(args, options);

                    if (options.Url != null)
                    {
                        NexusUrl url;
                        if (NexusUrl.TryParse(options.Url, out url))
                        {
                            app.HandleUrl(url);
                        }
                    }
                }

                app.Run();
#if !DEBUG
            }
            catch (Exception e)
            {
                // Report crash to the server.
                var crashReporterFactory = ObjectFactory.GetInstance <ICrashReporterFactory>();
                crashReporterFactory.Report("global", e);

                // Write the error to the log file.
                var log = ObjectFactory.GetInstance <ILogger>();
                log?.WriteLine("Application crashed!", LogLevel.Fatal);
                log?.WriteException(e);
            }
#endif
                return;
            }

            // If any arguments are set, pass these on to our main application instance.
            if (args.Any())
            {
                var attempts = 0;
                do
                {
                    try
                    {
                        // Write the specified arguments to a temporary ipc.dat file.
                        using (var fileStream = File.OpenWrite(Path.Combine(AppData.Path, "ipc.dat")))
                            using (var streamWriter = new StreamWriter(fileStream))
                            {
                                var options = new AppCommandLineOptions();
                                Parser.Default.ParseArguments(args, options);

                                if (options.Url != null)
                                {
                                    streamWriter.WriteLine(options.Url);
                                }
                            }

                        return;
                    }
                    catch (IOException)
                    {
                        // If storing the arguments fails, we're in trouble. Let's try it again in a few.
                        Thread.Sleep(500);
                        attempts++;
                    }
                } while (attempts < 5); // Limit to 5 attempts.
            }
        }