Esempio n. 1
0
        private static int RunApp()
        {
            if (CliArgs.ForceDebugMode)
            {
                App.ForceDebugMode = true;
            }

            if (CliArgs.IsProxyStart)
            {
                StartWithSpotify.HandleProxiedStart();
            }

            var app = new App();

            app.InitializeComponent();
            app.ShutdownMode = ShutdownMode.OnMainWindowClose;
            app.DispatcherUnhandledException      += (s, e) => OnUnhandledException(e.Exception);
            TaskScheduler.UnobservedTaskException += (s, e) =>
                                                     Logger.LogException("Unobserved task exception: ", e.Exception);

            var exitCode = app.Run();

            if (CliArgs.IsProxyStart)
            {
                StartWithSpotify.HandleProxiedExit();
            }

            return(exitCode);
        }
Esempio n. 2
0
        private static async Task RunPipeServer(CancellationToken cancellationToken)
        {
            using var server = new NamedPipeServerStream(PipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

            cancellationToken.Register(() => {
                if (server.IsConnected)
                {
                    server.Disconnect();
                }
                server.Dispose();
            });
            await server.WaitForConnectionAsync(cancellationToken);

            // we received a connection, which means another instance was started -> we bring the window to the front
            _ = Application.Current.Dispatcher.BeginInvoke(() => {
                var mainWindow = (MainWindow)Application.Current.MainWindow;
                mainWindow.Dispatcher.BeginInvoke(() => {
                    mainWindow.Deminimize();
                });
            });

            using var reader = new StreamReader(server);
            if (await reader.ReadLineAsync() is string line)
            {
                if (line == CliArgs.ProxyStartOption)
                {
                    StartWithSpotify.HandleProxiedStart();
                    if (!CliArgs.IsProxyStart)
                    {
                        CliArgs = CliArgs with {
                            IsProxyStart = true
                        }
                    }
                    ;
                }
            }

            server.Disconnect();

            // restart server
            await RunPipeServer(cancellationToken);
        }