Beispiel #1
0
        static void Main(string[] args)
        {
            var currentDomain = AppDomain.CurrentDomain;

            // Handler for unhandled exceptions.
            currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
            // Handler for exceptions in threads behind forms.
            Application.ThreadException += GlobalThreadExceptionHandler;

            var startUpParams = StartUpParams.Parse(args);

            if (startUpParams.ActivateChromeFontFix || startUpParams.DeactivateChromeFontFix)
            {
                Utils.InstallChromeFix(startUpParams.ActivateChromeFontFix);
                return;
            }

            string mutexId = $"Global\\{typeof(MainForm).GUID}";
            bool   mutexCreated;
            var    mutex = new Mutex(true, mutexId, out mutexCreated);

            try
            {
                if (!mutexCreated)
                {
                    MessageBox.Show("Only one instance of this program can be active.", "ColorControl");
                }
                else
                {
                    try
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new MainForm(startUpParams));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error while initializing application: " + ex.ToLogString(Environment.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                if (mutexCreated)
                {
                    mutex.Dispose();
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var currentDomain = AppDomain.CurrentDomain;

            // Handler for unhandled exceptions.
            currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
            // Handler for exceptions in threads behind forms.
            Application.ThreadException += GlobalThreadExceptionHandler;

            DataDir = Utils.GetDataPath();
            InitLogger();

            LoadConfig();

            if (Config.UseGdiScaling)
            {
                Application.SetHighDpiMode(HighDpiMode.DpiUnawareGdiScaled);
            }

            var startUpParams = StartUpParams.Parse(args);

            AppContext = new AppContext(Config, startUpParams);

            var existingProcess = Utils.GetProcessByName("ColorControl");

            try
            {
                if (HandleStartupParams(startUpParams, existingProcess))
                {
                    return;
                }
            }
            finally
            {
                Utils.CloseConsole();
            }

            string mutexId = $"Global\\{typeof(MainForm).GUID}";
            var    mutex   = new Mutex(true, mutexId, out var mutexCreated);

            try
            {
                if (!mutexCreated)
                {
                    if (existingProcess != null && existingProcess.Threads.Count > 0)
                    {
                        var thread = existingProcess.Threads[0];
                        NativeMethods.EnumThreadWindows((uint)thread.Id, EnumThreadWindows, IntPtr.Zero);

                        return;
                    }

                    MessageBox.Show("Only one instance of this program can be active.", "ColorControl");
                }
                else
                {
                    try
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new MainForm(AppContext));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error while initializing application: " + ex.ToLogString(Environment.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                if (mutexCreated)
                {
                    mutex.Dispose();
                }
            }
        }