private static void Main(string[] args) { Logger.Info($"TV Rename {Helpers.DisplayVersion} started with args: {string.Join(" ", args)}"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); AppDomain.CurrentDomain.UnhandledException += GlobalExceptionHandler; if (args.Contains("/?", StringComparer.OrdinalIgnoreCase)) { Logger.Info(CommandLineArgs.Helptext()); //redirect console output to parent process; //must be before any calls to Console.WriteLine() //MS: Never got this to work quite right - seems there is no simple way to output //to the command line console if you are a winforms app if (NativeMethods.AttachParentConsole()) { Console.WriteLine(CommandLineArgs.Helptext()); } else { Logger.Info("Could not attach to console"); } return; } // Check if an application instance is already running Mutex mutex = new Mutex(true, "TVRename", out bool newInstance); if (!newInstance) { // Already running Logger.Warn("An instance is already running"); // Create an IPC channel to the existing instance RemoteClient.Proxy(); // Transparent proxy to the existing instance RemoteClient ipc = new RemoteClient(); // If already running and no command line arguments then bring instance to the foreground and quit if (args.Length == 0) { ipc.FocusWindow(); return; } // Send command-line arguments to already running instance CommandLineArgs.MissingFolderBehavior previousMissingFolderBehavior = ipc.MissingFolderBehavior; bool previousRenameBehavior = ipc.RenameBehavior; // Parse command line arguments CommandLineArgs clargs = new CommandLineArgs(new ReadOnlyCollection <string>(args)); if (clargs.RenameCheck == false) { // Temporarily override behavior for renaming folders ipc.RenameBehavior = false; } if (clargs.MissingFolder != CommandLineArgs.MissingFolderBehavior.Ask) { // Temporarily override behavior for missing folders ipc.MissingFolderBehavior = clargs.MissingFolder; } // TODO: Unify command line handling between here and in UI.cs (ProcessArgs). Just send in clargs via IPC? if (clargs.Scan) { ipc.Scan(); } if (clargs.QuickScan) { ipc.QuickScan(); } if (clargs.RecentScan) { ipc.RecentScan(); } if (clargs.DoAll) { ipc.ProcessAll(); } if (clargs.Quit) { ipc.Quit(); } // TODO: Necessary? ipc.RenameBehavior = previousRenameBehavior; ipc.MissingFolderBehavior = previousMissingFolderBehavior; return; } #if !DEBUG try { #endif Logger.Info("Starting new instance"); new ApplicationBase().Run(args); GC.KeepAlive(mutex); #if !DEBUG } catch (Exception ex) { Logger.Error(ex, "Application exiting with error"); new ShowException(ex).ShowDialog(); Environment.Exit(1); } #endif Logger.Info("Application exiting"); }