communicating between windows applications

  • Thread starter Thread starter Gabe Moothart
  • Start date Start date
G

Gabe Moothart

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
 
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.
 
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.GetObject(typeof(IMLQShared),
"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
 
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
 
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.GetObject 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
 
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.

Steve,
I read in an online Remoting tutorial that all remoting objects must
inherit from MarshalByRefObject... however, Service2 already inherits
from ServiceBase, so it can't inherit from MarshalByRefObject. 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
 
Gabe said:
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.

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]
 
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 MarshalByRefObject and
implements ISharedRemoting. Let's call this class, MySharedRemoting. Set up
your remoting in Service2.

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

Believe me once you get this, it'll be simple trust me.
HTH
Steve
 
Steve,
Thanks for being so helpful. I realized right after I sent the post that
ServiceBase does, indeed, inherit from MarshalByRefObject (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 RegisterWellKnownServiceType() 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
 
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 MarshalByRefObject and
implements ISharedRemoting. Let's call this class, MySharedRemoting. Set up
your remoting in Service2.

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

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