Sending messages between program instances

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rwakelan@gmail.com

    Sending messages between program instances

    I am trying to create a program that implements the Singleton pattern.
    If another instance of the program is created, I want to send the
    information to the first instance and then close the new instance. I
    have a couple of different ways of keeping the second instance from
    opening up and they seem to work well. However, I am getting stuck
    whenever I try to send information from the second instance to the
    first instance. I have tried to set up .NET remoting using
    IrcServerChanne l and IrcClientChanne l, but I am not sure this is right
    because everything I see uses an external object and that doesn't make
    sense for what I am doing. Is there any way to do what I need to do?

  • fallenidol

    #2
    Re: Sending messages between program instances

    what does the 2nd instance need to send to first instance and why?

    <rwakelan@gmail .comwrote in message
    news:1162413209 .820088.137510@ h48g2000cwc.goo glegroups.com.. .
    >I am trying to create a program that implements the Singleton pattern.
    If another instance of the program is created, I want to send the
    information to the first instance and then close the new instance. I
    have a couple of different ways of keeping the second instance from
    opening up and they seem to work well. However, I am getting stuck
    whenever I try to send information from the second instance to the
    first instance. I have tried to set up .NET remoting using
    IrcServerChanne l and IrcClientChanne l, but I am not sure this is right
    because everything I see uses an external object and that doesn't make
    sense for what I am doing. Is there any way to do what I need to do?
    >

    Comment

    • Claus Konrad [MCSD]

      #3
      RE: Sending messages between program instances

      You might want to take a look at the Mutex class for system wide singletons.

      --
      rgds.
      /Claus Konrad
      MCSD.NET (C#)


      "rwakelan@gmail .com" wrote:
      I am trying to create a program that implements the Singleton pattern.
      If another instance of the program is created, I want to send the
      information to the first instance and then close the new instance. I
      have a couple of different ways of keeping the second instance from
      opening up and they seem to work well. However, I am getting stuck
      whenever I try to send information from the second instance to the
      first instance. I have tried to set up .NET remoting using
      IrcServerChanne l and IrcClientChanne l, but I am not sure this is right
      because everything I see uses an external object and that doesn't make
      sense for what I am doing. Is there any way to do what I need to do?
      >
      >

      Comment

      • Marc Gravell

        #4
        Re: Sending messages between program instances

        A bit like this? Rather that a Mutex (which would also work), this relies on
        only being able to bind one server to the port.

        using System;
        using System.Runtime. Remoting.Channe ls;
        using System.Runtime. Remoting;
        using System.Runtime. Remoting.Channe ls.Ipc;

        class Program
        {
        static void Main(string[] appArgs)
        {
        string message = string.Join("|" , appArgs);
        if (RemoteMessage. StartServer())
        { // new instance; start main app

        RemoteMessage.M essageReceived += delegate(object sender,
        RemoteMessage.R emoteMessageEve ntArgs remoteArgs)
        {
        Console.WriteLi ne("Received: " + remoteArgs.Mess age);
        };
        Console.WriteLi ne("Startup: " + message);
        Console.WriteLi ne("Press [Return] to end server");
        Console.ReadLin e();
        }
        else
        { // already running; forward the message
        RemoteMessage.S endMessage(mess age);
        Console.WriteLi ne("Forwarded: " + message);
        }
        }

        }

        public static class RemoteMessage
        {

        public static event EventHandler<Re moteMessageEven tArgs>
        MessageReceived ;

        private static void OnMessageReceiv ed(string message)
        {
        EventHandler<Re moteMessageEven tArgshandler = MessageReceived ;
        if (handler != null)
        {
        handler(typeof( RemoteMessage), new
        RemoteMessageEv entArgs(message ));
        }
        }

        public class RemoteMessageEv entArgs : EventArgs
        {
        public readonly string Message;
        public RemoteMessageEv entArgs(string message)
        {
        Message = message;
        }
        }
        public sealed class RemoteAgent : ContextBoundObj ect
        {
        public void ProcessMessage( string message)
        {
        RemoteMessage.O nMessageReceive d(message);
        }
        }

        private readonly static object _syncLock = new object();
        private static object SyncLock { get { return _syncLock; } }
        private static int _port = 32561;
        public static int Port
        {
        get { lock (SyncLock) { return _port; } }
        set { lock (SyncLock) { _port = value; } }
        }

        private static string _app = "Anon";
        public static string App
        {
        get { lock (SyncLock) { return _app; } }
        set
        {
        if (value == null) throw new ArgumentNullExc eption("App");
        lock (SyncLock)
        {
        _app = value;
        }
        }
        }

        public static bool StartServer()
        {
        try
        {
        int port = Port;
        string app = App;
        if (ChannelService s.GetChannel("i pc") == null)
        {
        ChannelServices .RegisterChanne l(new IpcChannel("127 .0.0.1:"
        + port.ToString() ), false);
        }

        WellKnownServic eTypeEntry serviceType = new
        WellKnownServic eTypeEntry(type of(RemoteAgent) , app,
        WellKnownObject Mode.Singleton) ;
        RemotingConfigu ration.Register WellKnownServic eType(serviceTy pe);

        return true;

        }
        catch (Exception e)
        {
        return false; // server already running
        }

        }

        public static void SimulateMessage (string message)
        {
        OnMessageReceiv ed(message);
        }

        public static bool SendMessage(str ing message, string server, string
        app, int port)
        {
        bool localChannel;
        IChannel channel = ChannelServices .GetChannel("ip c");
        localChannel = channel == null;

        string uri = string.Format(" ipc://{2}:{0}/{1}", port, app, server);

        if (localChannel)
        {
        channel = new IpcChannel();
        ChannelServices .RegisterChanne l(channel, false);
        }
        try
        {
        if
        (RemotingConfig uration.IsWellK nownClientType( typeof(RemoteAg ent)) == null)
        {
        WellKnownClient TypeEntry serviceType = new
        WellKnownClient TypeEntry(typeo f(RemoteAgent), uri);
        RemotingConfigu ration.Register WellKnownClient Type(serviceTyp e);

        }
        RemoteAgent agent =
        (RemoteAgent)Ac tivator.GetObje ct(typeof(Remot eAgent), uri);
        try
        {
        agent.ProcessMe ssage(message);
        return true;
        }
        catch
        {
        return false;
        }
        }
        finally
        {
        if (localChannel)
        {
        ChannelServices .UnregisterChan nel(channel);
        }
        }
        }
        public static bool SendMessage(str ing message)
        {
        return SendMessage(mes sage, "127.0.0.1" , App, Port);
        }
        }


        Comment

        Working...