Example #1
0
        static void Main()
        {
            try
            {
                using (var instance = new SingleGlobalInstance(2000))
                {
                    if (!System.Diagnostics.Debugger.IsAttached)
                    {
                        AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
                        Application.ThreadException += OnThreadException;
                        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

                        try
                        {
                            main();
                        }
                        catch (Exception ex)
                        {
                            LogException(ex);
                            throw;
                        }
                    }
                    else
                    {
                        main();
                    }
                }
            }
            catch (TimeoutException)
            {
                // find the window or notify window
                foreach (var process in Process.GetProcesses())
                {
                    if (process.ProcessName == APPLICATION_NAME)
                    {
                        process.Refresh();

                        var hwnd = process.MainWindowHandle;
                        if (hwnd == (IntPtr)0)
                        {
                            hwnd = WinAPI.FindWindow(null, APPLICATION_TITLE);
                        }

                        // send it the open message
                        WinAPI.SendMessage(hwnd, WinAPI.WM_USER + 1, 0, (IntPtr)0);
                        return;
                    }
                }

                // fallback
                MessageBox.Show(string.Format(strings.AlreadyRunning, APPLICATION_NAME), APPLICATION_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
Example #2
0
        static void Main()
        {
            try
            {
                // configure Logger
                var config = new LoggingConfiguration();
                //
                var    fileTarget = new FileTarget();
                string dir        = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), WinAuthMain.APPLICATION_NAME);
                if (Directory.Exists(dir) == false)
                {
                    dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                }
                fileTarget.FileName = Path.Combine(dir, "winauth.log");
                fileTarget.Layout   = @"${longdate} ${assembly-version} ${logger} ${message} ${exception:format=tostring}";
                fileTarget.DeleteOldFileOnStartup = false;
                fileTarget.AutoFlush = true;
                config.AddTarget("file", fileTarget);
                //
                var rule = new LoggingRule("*", LogLevel.Error, fileTarget);
                config.LoggingRules.Add(rule);
                //
                LogManager.Configuration = config;
                Logger = LogManager.GetLogger(APPLICATION_NAME);

                using (var instance = new SingleGlobalInstance(2000))
                {
                    if (!System.Diagnostics.Debugger.IsAttached)
                    {
                        AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
                        Application.ThreadException += OnThreadException;
                        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

                        try
                        {
                            main();
                        }
                        catch (Exception ex)
                        {
                            LogException(ex);
                            throw;
                        }
                    }
                    else
                    {
                        main();
                    }
                }
            }
            catch (TimeoutException)
            {
                // find the window or notify window
                foreach (var process in Process.GetProcesses())
                {
                    if (process.ProcessName == APPLICATION_NAME)
                    {
                        process.Refresh();

                        var hwnd = process.MainWindowHandle;
                        if (hwnd == (IntPtr)0)
                        {
                            hwnd = WinAPI.FindWindow(null, APPLICATION_TITLE);
                        }

                        // send it the open message
                        WinAPI.SendMessage(hwnd, WinAPI.WM_USER + 1, 0, (IntPtr)0);
                        return;
                    }
                }

                // fallback
                MessageBox.Show(string.Format(strings.AlreadyRunning, APPLICATION_NAME), APPLICATION_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }