IPC Mechanisms in C# - Usage and Best Practices
Asked Answered
B

6

56

I have used IPC in Win32 code a while ago - critical sections, events, and semaphores.

How is the scene in the .NET environment? Are there any tutorial explaining all available options and when to use and why?

Beira answered 11/9, 2008 at 9:24 Comment(2)
What do you need to do? If you need to synchronize access to some external resource, you can use a Mutex to implement cross-process synchronization.Roper
+1. Thanks god. This is the first instance of questions with "Best Practices" flavor which is not marked as non-constructive/off-topic [that I have ever seen] !!Trail
A
31

Most recent Microsoft's stuff in IPC is Windows Communication Foundation. Actually there is nothing new in the lower level (tcp, upd, named pipes etc) But WCF simplifies IPC development greatly.

Useful resource:

and of course MSDN on WCF

Asyut answered 11/9, 2008 at 9:34 Comment(1)
Hey the video in the first link is dead!Phillips
A
12

Apart from the obvious (WCF), there is a ZeroMQ binding for C#/CLR which is pretty good:

http://www.zeromq.org/bindings:clr

Does message-oriented IPC, pub/sub and various other strategies with much less code and config than WCF.

It's also at least an order of magnitude faster than anything else and has less latency if you require low latency comms.

With respects to semaphores, locks, mutexes etc. If you share by communicating rather than communicate by sharing, you'll have a whole load less hassle than the traditional paradigm.

Astute answered 27/9, 2011 at 16:36 Comment(5)
"share by communicating rather than communicate by sharing" This should be recited every day by anyone who is doing concurrent programming.Encephalo
What does this mean?Succinctorium
I take it to mean that instead of sharing resources and having each program try to claim it at will, you should have programs talk directly to each other with IPC. In the first scenario, your programs "communicate" by locking a shared resource. In the second, they communicate through some protocol to coordinate usage of the shared resource.Playwriting
zguide.zeromq.org/page:all#Unicast-Transports The inter-process ipc transport is disconnected, like tcp. It has one limitation: it does not yet work on Windows. Soooo actually not really IPC for windows right? Which still might be quite common when the C# tag was added.Incombustible
You could use the local loopback adaptor with ZeroMQ. Performance will obviously depend on the adaptor implementation though. However, I don't like the semantics of hitting the network stack (albeit a virtual interface) when your intention is not communication over a network. Personally for IPC on the same machine I think using pipes is the best solution. The .NET Standard implementation is rock solid and totally portable. Mapped memory solutions may offer the best performance, but rolling your own is complex and synchronisation is error prone. "share by communicating" :)Schistosome
I
10

I tend to use named pipes or Unix sockets (depending on whether I'm targetting MS.NET or Mono -- I have a class that abstracts it away) since it's easy to use, portable, and allows me to easily interoperate with unmanaged code. That said, if you're only dealing with managed code, go with WCF or remoting -- the latter if you need Mono support, since their WCF support simply isn't there yet.

Imbecile answered 11/9, 2008 at 10:0 Comment(1)
While this may have been the case in Sep 2008 (when this comment was made), it's 2011 and WCF on Mono is quite mature now. See the Mono WCF Development page and judge for yourself.Uniformitarian
D
7

I would recommend using Memory Mapped Files if you need to use on the machine domain not communication through network. See the following link.

http://techmikael.blogspot.com/2010/02/blazing-fast-ipc-in-net-4-wcf-vs.html

Diplegia answered 3/1, 2012 at 13:18 Comment(1)
A list of .NET local Machine APIs: weblogs.asp.net/ricardoperes/…Weiss
G
3

There is also .NET Remoting, which I found quite cool, but I guess they are obsoleting it now that they have WCF.

Gelb answered 11/9, 2008 at 9:38 Comment(3)
Has Microsoft ever actually said that?Martinamartindale
They have: "This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF)." from msdn.microsoft.com/en-us/library/kwdt6w2k.aspxChoirmaster
I have worked with Remoting for years.. For small-scale applications, ( e.g. passing up to 1k small objects ) it is very straight-forward and efficient. Above that limit, No! It really sucks. You have to zip data before sending and this adds extra overhead ( CPU usage ). I had a very frustrating memory with the issue.Yachting
E
3

It sounds as though you're interested in synchronization techniques rather than communication. If so, you might like to start here, or perhaps this more concise overview.

Excise answered 11/9, 2008 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.