communicating between windows applications

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gabe Moothart

    communicating between windows applications

    Hi,
    I have 3 applications (2 services and a winforms app) that need to be
    able to send/recieve messages from each other. What is the best way to
    do this in .NET? I looked briefly at remoting, but it seems like it
    might be overkill since the applications are all on the same machine. I
    could use an xml file which they can all read/write to... but I was
    wondering what best practice for this kind of thing is in .NET. Any ideas?

    TIA,
    Gabe
  • Derrick

    #2
    Re: communicating between windows applications

    Maybe you should check out the Message Queue (MSMQ)?

    Derrick

    "Gabe Moothart" <gabe@imaginesy stems.net> wrote in message
    news:Ow3kcLFaFH A.3384@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi,
    > I have 3 applications (2 services and a winforms app) that need to be able
    > to send/recieve messages from each other. What is the best way to do this
    > in .NET? I looked briefly at remoting, but it seems like it might be
    > overkill since the applications are all on the same machine. I could use
    > an xml file which they can all read/write to... but I was wondering what
    > best practice for this kind of thing is in .NET. Any ideas?
    >
    > TIA,
    > Gabe[/color]


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: communicating between windows applications

      Gabe,

      I don't think that remoting is really a bad idea. It will allow you to
      set specific operations, not have to worry about parsing messages, etc, etc.
      Also, with .NET 2.0, there is a VERY fast cross-process channel specifically
      for communicating between two processes on the same machine.

      I would look at remoting, it really does make things a lot easier from a
      usability standpoint.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Gabe Moothart" <gabe@imaginesy stems.net> wrote in message
      news:Ow3kcLFaFH A.3384@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi,
      > I have 3 applications (2 services and a winforms app) that need to be able
      > to send/recieve messages from each other. What is the best way to do this
      > in .NET? I looked briefly at remoting, but it seems like it might be
      > overkill since the applications are all on the same machine. I could use
      > an xml file which they can all read/write to... but I was wondering what
      > best practice for this kind of thing is in .NET. Any ideas?
      >
      > TIA,
      > Gabe[/color]


      Comment

      • Steve Long

        #4
        Re: communicating between windows applications

        Boy, I suppose you could check out MSMQ but .NET Remoting is very slick and
        really pretty darned easy to implement. Here's one of the ways in which I
        have implemented it.
        Create a seperate assembly with an Interface defined in it that all apps
        that will need to commuicate with each other implement.
        Have the app that performs some function implement the interface, while the
        apps that need to call this function use Activator to return an object of
        this type. Then they just call the function. Easy.

        So, the app that needs to have another app either perform some function or
        just return some data from it does something like this:

        IMLQShared ishared = (IMLQShared) Activator.GetOb ject(typeof(IML QShared),
        "tcp://" + m_sRemoteIP + ":" +
        m_sRemotePort + "/RemoteMLQ.rem") ;

        Where IMLQShared is the Interface defined in the seperate assembly and
        RemoteMLQ is the object in the receiving app that performs the functionality
        that implements IMLQShared.
        I can provide an example of how to set up the receiving app too if you like.

        HTH
        Steve

        "Gabe Moothart" <gabe@imaginesy stems.net> wrote in message
        news:Ow3kcLFaFH A.3384@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Hi,
        > I have 3 applications (2 services and a winforms app) that need to be
        > able to send/recieve messages from each other. What is the best way to
        > do this in .NET? I looked briefly at remoting, but it seems like it
        > might be overkill since the applications are all on the same machine. I
        > could use an xml file which they can all read/write to... but I was
        > wondering what best practice for this kind of thing is in .NET. Any ideas?
        >
        > TIA,
        > Gabe[/color]


        Comment

        • Gabe Moothart

          #5
          Re: communicating between windows applications

          Thanks for the responses, everyone. One of the things I was worried
          about with remoting was the possible performance overhead of using tcp
          to communicate between processes on the same computer. But it's not
          mission-critical, and I can just switch to the inter-process channel
          once .NET 2.0 gets here.

          However, I'm having trouble getting remoting to do what I want to do.
          Service1 needs to be able to set a flag or send a message to service2,
          telling it to shut itself down as soon as it is safe to do so. Service2
          is running in a timer loop, and would check for the presence of the
          message/flag/whatever each timer interval.

          But using remoting I can only figure out how to execute a function in
          some class in the other process... not affect any variables visible to
          Service2.

          TIA,
          Gabe

          Comment

          • Steve Long

            #6
            Re: communicating between windows applications

            Okay, so you create this shared Interface that Service2 implements with a
            function in it. In the implementation of the function in Service2, set the
            flag. In Service1, call the function after you have created an object of the
            type of the shared interface using Activator.GetOb ject as I explained in my
            code of my previous post. Service1 will have a reference to the assembly of
            the shared interface so intellisense will show you the function after you
            use the dot. Get it?
            So, this should work unless I'm not understanding your scenario.
            Create a seperate assembly and define the interface. Compile.
            Set a reference to the assembly in Service2 and create a class that
            implements this interface.
            Set a reference to the assembly in Service1 and use the Activator to create
            an object of that type. Call the function. That's it pretty much. A few
            other remoting lines of code and you're there.

            Let me know if I have been less that clear.
            Steve

            "Gabe Moothart" <gabe@imaginesy stems.net> wrote in message
            news:%23UwWaeIa FHA.3712@TK2MSF TNGP09.phx.gbl. ..[color=blue]
            > Thanks for the responses, everyone. One of the things I was worried
            > about with remoting was the possible performance overhead of using tcp
            > to communicate between processes on the same computer. But it's not
            > mission-critical, and I can just switch to the inter-process channel
            > once .NET 2.0 gets here.
            >
            > However, I'm having trouble getting remoting to do what I want to do.
            > Service1 needs to be able to set a flag or send a message to service2,
            > telling it to shut itself down as soon as it is safe to do so. Service2
            > is running in a timer loop, and would check for the presence of the
            > message/flag/whatever each timer interval.
            >
            > But using remoting I can only figure out how to execute a function in
            > some class in the other process... not affect any variables visible to
            > Service2.
            >
            > TIA,
            > Gabe[/color]


            Comment

            • Gabe Moothart

              #7
              Re: communicating between windows applications

              > Okay, so you create this shared Interface that Service2 implements with a[color=blue]
              > function in it. In the implementation of the function in Service2,[/color]
              set the[color=blue]
              > flag.[/color]

              Steve,
              I read in an online Remoting tutorial that all remoting objects must
              inherit from MarshalByRefObj ect... however, Service2 already inherits
              from ServiceBase, so it can't inherit from MarshalByRefObj ect. So, I
              thought that I had to use a different object than Service2 for the
              actual remoting. Is this incorrect?

              Thanks for the help,
              Gabe

              Comment

              • Reginald Blue

                #8
                Re: communicating between windows applications

                Gabe Moothart wrote:[color=blue]
                > Service1 needs to be able to set a flag or send a message to service2,
                > telling it to shut itself down as soon as it is safe to do so.
                > Service2 is running in a timer loop, and would check for the presence
                > of the message/flag/whatever each timer interval.[/color]

                Couldn't you just use a named Mutex then? That would seem like the easiest
                way, and then it's just waiting on the Mutex to shut down.

                --
                Reginald Blue
                "I have always wished that my computer would be as easy to use as my
                telephone. My wish has come true. I no longer know how to use my
                telephone."
                - Bjarne Stroustrup (originator of C++) [quoted at the 2003
                International Conference on Intelligent User Interfaces]


                Comment

                • Steve Long

                  #9
                  Re: communicating between windows applications

                  Gabe, real quick before I shoot out the door for the weekend.
                  In Service2 you are going to have a reference to the Shared assembly that
                  you create. Let's call this assembly SharedRemoting with an interface called
                  ISharedRemoting .
                  Define an interface in this assembly.
                  Create a class in Service2 that inherits from MarshalByRefObj ect and
                  implements ISharedRemoting . Let's call this class, MySharedRemotin g. Set up
                  your remoting in Service2.

                  In Service1 also have a reference to SharedRemoting and get an object of
                  type ISharedRemoting from Activator.GetOb ject as described in my earlier
                  post. Call the function that you defined in the interface and implemented in
                  MySharedRemotin g. In this function, set the flag.

                  Believe me once you get this, it'll be simple trust me.
                  HTH
                  Steve

                  "Gabe Moothart" <gabe@imaginesy stems.net> wrote in message
                  news:uPI3g0IaFH A.1088@TK2MSFTN GP14.phx.gbl...[color=blue][color=green]
                  > > Okay, so you create this shared Interface that Service2 implements with[/color][/color]
                  a[color=blue][color=green]
                  > > function in it. In the implementation of the function in Service2,[/color]
                  > set the[color=green]
                  > > flag.[/color]
                  >
                  > Steve,
                  > I read in an online Remoting tutorial that all remoting objects must
                  > inherit from MarshalByRefObj ect... however, Service2 already inherits
                  > from ServiceBase, so it can't inherit from MarshalByRefObj ect. So, I
                  > thought that I had to use a different object than Service2 for the
                  > actual remoting. Is this incorrect?
                  >
                  > Thanks for the help,
                  > Gabe[/color]


                  Comment

                  • Gabe Moothart

                    #10
                    Re: communicating between windows applications

                    Steve,
                    Thanks for being so helpful. I realized right after I sent the post that
                    ServiceBase does, indeed, inherit from MarshalByRefObj ect (d'oh!). After
                    a little bit of debugging, I got everything working. All I can say is...
                    wow. I am impressed.

                    One question, though: the RegisterWellKno wnServiceType() call takes as
                    an argument the type of object to register, not the specific instance.
                    So how does .NET know what instance I want my remote calls applied to?

                    Gabe

                    Comment

                    • Steve Long

                      #11
                      Re: communicating between windows applications

                      Gabe, I sent this Friday before I left. I don't know why it didn't show up
                      in the group.

                      Gabe, real quick before I shoot out the door for the weekend.
                      In Service2 you are going to have a reference to the Shared assembly that
                      you create. Let's call this assembly SharedRemoting with an interface called
                      ISharedRemoting .
                      Define an interface in this assembly.
                      Create a class in Service2 that inherits from MarshalByRefObj ect and
                      implements ISharedRemoting . Let's call this class, MySharedRemotin g. Set up
                      your remoting in Service2.

                      In Service1 also have a reference to SharedRemoting and get an object of
                      type ISharedRemoting from Activator.GetOb ject as described in my earlier
                      post. Call the function that you defined in the interface and implemented in
                      MySharedRemotin g. In this function, set the flag.

                      Believe me once you get this, it'll be simple trust me.
                      HTH
                      Steve

                      "Gabe Moothart" <gabe@imaginesy stems.net> wrote in message
                      news:uPI3g0IaFH A.1088@TK2MSFTN GP14.phx.gbl...[color=blue][color=green]
                      > > Okay, so you create this shared Interface that Service2 implements with[/color][/color]
                      a[color=blue][color=green]
                      > > function in it. In the implementation of the function in Service2,[/color]
                      > set the[color=green]
                      > > flag.[/color]
                      >
                      > Steve,
                      > I read in an online Remoting tutorial that all remoting objects must
                      > inherit from MarshalByRefObj ect... however, Service2 already inherits
                      > from ServiceBase, so it can't inherit from MarshalByRefObj ect. So, I
                      > thought that I had to use a different object than Service2 for the
                      > actual remoting. Is this incorrect?
                      >
                      > Thanks for the help,
                      > Gabe[/color]


                      Comment

                      Working...