Exemplo n.º 1
0
        /// <summary>
        /// Sends a message to the first instance of the application.
        /// </summary>
        /// <param name="message">The message to send to the first instance of the application. The message must be serializable.</param>
        /// <exception cref="System.InvalidOperationException">The object was constructed with the SingleInstanceTracker(string name) constructor overload, or with the SingleInstanceTracker(string name, SingleInstanceEnforcerRetriever enforcerRetriever) cosntructor overload, with enforcerRetriever set to null.</exception>
        /// <exception cref="SingleInstancing.SingleInstancingException">The SingleInstanceInteractor has failed to send the message to the first application instance. The first instance might have terminated.</exception>
        public void SendMessageToFirstInstance(object message)
        {
            #if IPC_SUPPORTS
            if (fIpcChannel == null)
            {
                throw new InvalidOperationException("The object was constructed with the SingleInstanceTracker(string name) constructor overload, or with the SingleInstanceTracker(string name, SingleInstanceEnforcerRetriever enforcerRetriever) constructor overload, with enforcerRetriever set to null, thus you cannot send messages to the first instance.");
            }

            try {
                fProxy.Enforcer.OnMessageReceived(new MessageEventArgs(message));
            } catch (Exception ex) {
                throw new SingleInstancingException("Failed to send message to the first instance of the application. The first instance might have terminated.", ex);
            }
            #else
            try {
                string[] args = message as string[];
                if (args.Length == 0 || string.IsNullOrEmpty(args[0]))
                {
                    IpcFake.Send(AppMessage.RestoreWindow, 0, false);
                }
                else
                {
                    IpcFake.SendMessage(IpcFake.CmdSendArgs, args);
                }
            } catch (Exception ex) {
                Logger.LogWrite("SingleInstanceTracker.SendMessageToFirstInstance.2(): " + ex.Message);
            }
            #endif
        }
Exemplo n.º 2
0
        public void Test_IpcFake()
        {
            IpcFake.StartServer(GetSingleInstanceEnforcer());

            Assert.AreEqual(true, IpcFake.CreateMutex("test", true));
            IpcFake.RefreshMutexes();

            Assert.AreEqual(false, IpcFake.CreateMutex("test", true));
            IpcFake.RefreshMutexes();

            Assert.Throws(typeof(ArgumentNullException), () => { IpcFake.SendMessage(null); });

            IpcFake.SendMessage(IpcFake.CmdSendArgs, new string[] { "testArgX" });

            IpcFake.StopServer();
            IpcFake.ReleaseAllMutexes();
        }
Exemplo n.º 3
0
        public static void SendMessage(string cmd, string[] args)
        {
            IpcParamEx ipcMsg = new IpcParamEx(cmd, IpcFake.SafeSerialize(args));

            IpcFake.SendMessage(ipcMsg);
        }