Exemple #1
0
        /// <summary>
        /// Instantiates a new SingletonProxy object.
        /// </summary>
        /// <param name="enforcer">The enforcer (first instance of the application) which will receive messages from the new instances of the application.</param>
        /// <exception cref="ArgumentNullException">Thrown when the enforcer is null.</exception>
        public SingletonProxy(ISingletonEnforcer enforcer)
        {
            if (enforcer == null)
            {
                throw new ArgumentNullException("enforcer", "enforcer cannot be null.");
            }

            _enforcer = enforcer;
        }
Exemple #2
0
        /// <summary>
        /// Instantiates a new SingletonController object to determine whether the application is already running.
        /// </summary>
        /// <param name="name">The unique name used to identify the application.</param>
        /// <param name="enforcer">The ISingletonEnforcer object that will handle messages.</param>
        /// <exception cref="System.ArgumentNullException">name is null or empty.</exception>
        /// <exception cref="SingletonException">A general error occured while trying to instantiate the SingletonController.</exception>
        public SingletonController(string name, ISingletonEnforcer enforcer)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "name cannot be null or empty.");
            }

            if (enforcer == null)
            {
                throw new InvalidOperationException("the ISingletonInstanceEnforcer cannot be null");
            }

            try
            {
                _singleInstanceMutex = new Mutex(true, name, out _isFirstInstance);

                string proxyObjectName = "SingletonProxy";
                string proxyUri        = "ipc://" + name + "/" + proxyObjectName;

                // If no previous instance was found, create a server channel which will provide the proxy to the first created instance
                if (_isFirstInstance)
                {
                    // Create an IPC server channel to listen for SingletonProxy object requests
                    _ipcChannel = new IpcServerChannel(name);
                    // Register the channel and get it ready for use
                    ChannelServices.RegisterChannel(_ipcChannel, false);
                    // Register the service which gets the SingletonProxy object, so it can be accessible by IPC client channels
                    RemotingConfiguration.RegisterWellKnownServiceType(typeof(SingletonProxy), proxyObjectName, WellKnownObjectMode.Singleton);

                    // Create the first proxy object
                    _proxy = new SingletonProxy(enforcer);
                    // Publish the first proxy object so IPC clients requesting a proxy would receive a reference to it
                    RemotingServices.Marshal(_proxy, proxyObjectName);
                }
                else
                {
                    // Create an IPC client channel to request the existing SingletonProxy object.
                    _ipcChannel = new IpcClientChannel();
                    // Register the channel and get it ready for use
                    ChannelServices.RegisterChannel(_ipcChannel, false);

                    // Retreive a reference to the proxy object which will be later used to send messages
                    _proxy = (SingletonProxy)Activator.GetObject(typeof(SingletonProxy), proxyUri);
                }
            }
            catch (Exception ex)
            {
                throw new SingletonException("Failed to instantiate a new SingletonController object. See InnerException for more details.", ex);
            }
        }