Exemplo n.º 1
0
        private static int Main(string[] args)
        {
            bool running = true;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // This might fix the SEHException raised sometimes. See issue:
            // https://sourceforge.net/tracker/?func=detail&aid=2335753&group_id=96589&atid=615248
            Application.DoEvents();

            // child threads should impersonate the current windows user
            AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);

            /* setup handler for unhandled exceptions in non-debug modes */
            // Allow exceptions to be unhandled so they break in the debugger
#if !DEBUG
            ApplicationExceptionHandler eh = new ApplicationExceptionHandler();

            AppDomain.CurrentDomain.UnhandledException += eh.OnAppDomainException;
#endif

#if DEBUG && TEST_I18N_THISCULTURE
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(new I18NTestCulture().Culture);
            Thread.CurrentThread.CurrentCulture   = Thread.CurrentThread.CurrentUICulture;
#endif

            FormWindowState initialStartupState = Win32.GetStartupWindowState();
            // if you want to debug the minimzed startup (cannot be configured in VS.IDE),
            // comment out the line above and uncomment the next one:
            //FormWindowState initialStartupState =  FormWindowState.Minimized;

            RssBanditApplication  appInstance = new RssBanditApplication();
            OtherInstanceCallback callback    = appInstance.OnOtherInstance;
            try
            {
                running = InitialInstanceActivator.Activate(appInstance, callback, args);
            }
            catch (Exception /* ex */)
            {
                //_log.Error(ex); /* other instance is probably still running */
            }
            //_log.Info("Application v" + RssBanditApplication.VersionLong + " started, running instance is " + running);

            RssBanditApplication.StaticInit(appInstance);
            if (!running)
            {
                // init to system default:
                RssBanditApplication.SharedCulture   = CultureInfo.CurrentCulture;
                RssBanditApplication.SharedUICulture = CultureInfo.CurrentUICulture;

                if (appInstance.HandleCommandLineArgs(args))
                {
                    if (!string.IsNullOrEmpty(appInstance.CommandLineArgs.LocalCulture))
                    {
                        try
                        {
                            RssBanditApplication.SharedUICulture =
                                CultureInfo.CreateSpecificCulture(appInstance.CommandLineArgs.LocalCulture);
                            RssBanditApplication.SharedCulture = RssBanditApplication.SharedUICulture;
                        }
                        catch (Exception ex)
                        {
                            appInstance.MessageError(String.Format(
                                                         SR.ExceptionProcessCommandlineCulture,
                                                         appInstance.CommandLineArgs.LocalCulture,
                                                         ex.Message));
                        }
                    }

                    // take over customized cultures to current main thread:
                    Thread.CurrentThread.CurrentCulture   = RssBanditApplication.SharedCulture;
                    Thread.CurrentThread.CurrentUICulture = RssBanditApplication.SharedUICulture;

                    if (!appInstance.CommandLineArgs.StartInTaskbarNotificationAreaOnly &&
                        initialStartupState != FormWindowState.Minimized)
                    {
                        // no splash, if start option is tray only or minimized
                        Splash.Show(SR.AppLoadStateLoading,
                                    String.Format(String.IsNullOrEmpty(RssBanditApplication.versionPostfix) ? "v{0}": "v{0} {1}",
                                                  RssBanditApplication.Version, RssBanditApplication.versionPostfix)
                                    );
                    }

                    if (appInstance.Init())
                    {
                        // does also run the windows event loop:
                        appInstance.StartMainGui(initialStartupState);
                        Splash.Close();
                    }
                    else
                    {
                        return(3); // init error
                    }
                    return(0);     // OK
                }
                return(2);         // CommandLine error
            }
            return(1);             // other running instance
        }
Exemplo n.º 2
0
        public static bool Activate(ApplicationContext context, OtherInstanceCallback callback, string[] args)
        {
            // Check for existing instance
            bool createdNew = false;

            mutex = new Mutex(true, MutexName, out createdNew);

            if (!createdNew)
            {
                // Second instance
                // Open remoting channel exposed from initial instance
                string            url       = string.Format("tcp://localhost:{0}/{1}", Port, ChannelName);
                MainFormActivator activator = (MainFormActivator)RemotingServices.Connect(typeof(MainFormActivator), url);

                // Send arguments to initial instance and exit this one
                activator.OnOtherInstance(args);
                return(true);
            }

            // initial instance code...
            bool success = false; int maxRetry = 25;

            while (!success && maxRetry > 0)
            {
                try {
                    // Expose remoting channel to accept arguments from other instances
                    ChannelServices.RegisterChannel(new TcpChannel(Port), false);
                    success = true;
                } catch (System.Net.Sockets.SocketException sx) {
                    maxRetry--;
                    if (maxRetry > 0 && sx.ErrorCode == 10048)                    // WSAEADDRINUSE (10048) Address already in use.
                    {
                        usedPort++;
                        Win32.Registry.InstanceActivatorPort = usedPort;
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            /* for .NET 1.1 we may have to apply this fix
             * to workaround the exception:
             * "An unhandled exception of type 'System.Runtime.Serialization.SerializationException'
             * occurred in mscorlib.dll.
             * Additional information: Because of security restrictions, the type System.Runtime.Remoting.ObjRef
             * cannot be accessed."
             *
             *              IDictionary prop = new Hashtable();
             *              prop["bindTo"] = "127.0.0.1";
             *              prop["port"] = 0; // pick a random open port
             *              // need to add this due to issues with security and .net 1.1
             *              prop["typeFilterLevel"] = "Full";
             *
             *              // need to add this due to issues with security and .net 1.1
             *              BinaryServerFormatterSinkProvider provider = new
             *               BinaryServerFormatterSinkProvider();
             *              provider.TypeFilterLevel = Formatters.TypeFilterLevel.Full;
             *              TcpChannel tcp = new TcpChannel(prop, null, provider);
             *              ChannelServices.RegisterChannel(tcp);
             *
             */

            RemotingServices.Marshal(new MainFormActivator(context, callback), ChannelName);
            return(false);
        }
Exemplo n.º 3
0
 public MainFormActivator(ApplicationContext context, OtherInstanceCallback callback)
 {
     this.context  = context;
     this.callback = callback;
 }
Exemplo n.º 4
0
 public static bool Activate(Form mainForm, OtherInstanceCallback callback, string[] args)
 {
     return(Activate(new ApplicationContext(mainForm), callback, args));
 }