90

I want to create a communication between a parent and a child process, both written in C#.

It should be asynchronous, event-driven.

I don't want to run a thread in every process to handle the very rare communication.

What is the best solution for it?

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Horcrux7
  • 23,758
  • 21
  • 98
  • 156
  • 2
    I've done an article on my blog of a really simple example of inter-process communication using Named Pipes in WCF. [https://dopeydev.com/wcf-interprocess-communication/](https://dopeydev.com/wcf-interprocess-communication/) – Riain McAtamney Aug 09 '16 at 14:16
  • Are you talking about asynchronous processes or asynchronous communication? The first one can be done with WCF or WebServices or anything over tcp or pipes or anything. But asynchronous communication requires a queuing technology like MSMQ or Azure Service Bus or RabbitMQ. – Dennis van der Stelt Jun 18 '18 at 15:27
  • [Related: What is the easiest way to do inter process communication in C#?](https://stackoverflow.com/questions/1802475/what-is-the-easiest-way-to-do-inter-process-communication-in-c) – Sen Jacob Apr 26 '19 at 08:39
  • 1
    @RiainMcAtamney -- link died. :( – Jesse Chisholm Oct 03 '21 at 15:28

9 Answers9

56

Anonymous pipes.

Use Asynchronous operations with BeginRead/BeginWrite and AsyncCallback.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Carlos A. Ibarra
  • 6,002
  • 1
  • 28
  • 38
23

If your processes in same computer, you can simply use stdio.

This is my usage, a web page screenshooter:

var jobProcess = new Process();

jobProcess.StartInfo.FileName = Assembly.GetExecutingAssembly().Location;
jobProcess.StartInfo.Arguments = "job";

jobProcess.StartInfo.CreateNoWindow = false;
jobProcess.StartInfo.UseShellExecute = false;

jobProcess.StartInfo.RedirectStandardInput = true;
jobProcess.StartInfo.RedirectStandardOutput = true;
jobProcess.StartInfo.RedirectStandardError = true;

// Just Console.WriteLine it.
jobProcess.ErrorDataReceived += jp_ErrorDataReceived;

jobProcess.Start();

jobProcess.BeginErrorReadLine();

try
{
    jobProcess.StandardInput.WriteLine(url);
    var buf = new byte[int.Parse(jobProcess.StandardOutput.ReadLine())];
    jobProcess.StandardOutput.BaseStream.Read(buf, 0, buf.Length);
    return Deserz<Bitmap>(buf);
}
finally
{
    if (jobProcess.HasExited == false)
        jobProcess.Kill();
}

Detect args on Main

static void Main(string[] args)
{
    if (args.Length == 1 && args[0]=="job")
    {
        //because stdout has been used by send back, our logs should put to stderr
        Log.SetLogOutput(Console.Error); 

        try
        {
            var url = Console.ReadLine();
            var bmp = new WebPageShooterCr().Shoot(url);
            var buf = Serz(bmp);
            Console.WriteLine(buf.Length);
            System.Threading.Thread.Sleep(100);
            using (var o = Console.OpenStandardOutput())
                o.Write(buf, 0, buf.Length);
        }
        catch (Exception ex)
        {
            Log.E("Err:" + ex.Message);
        }
    }
    //...
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
IlPADlI
  • 1,943
  • 18
  • 22
  • 5
    Won't work however if the target process needs to have a GUI, i.e. if `ProcessStartInfo.UseShellExecute` is `true`. In that case you cannot redirect standard output and error. – Guido Domenici Aug 27 '15 at 12:29
  • @JayCroghan i don't think you can use it when it has a GUI. At least when the client process is launched from a web application. – Mr Balanikas Aug 02 '17 at 06:08
9

I would suggest using the Windows Communication Foundation:

http://en.wikipedia.org/wiki/Windows_Communication_Foundation

You can pass objects back and forth, use a variety of different protocols. I would suggest using the binary tcp protocol.

bobwienholt
  • 17,420
  • 3
  • 40
  • 48
7

Named pipes on WCF.

http://msdn.microsoft.com/en-us/library/ms733769.aspx

Amy B
  • 108,202
  • 21
  • 135
  • 185
4

If possible, it's just easier to make a shared file!

//out
File.AppendAllText("sharedFile.txt", "payload text here");
// in
File.ReadAllText("sharedFile.txt");
Kiraa
  • 495
  • 4
  • 11
  • 6
    Or you could bang two rocks together... this method would require polling and extra error handling around locked files etc. and is NOT event driven. Sure, it could work, but... – LordWabbit Apr 20 '22 at 09:28
  • I think this is not such a bad answer. .NET supplies a very neat class to make this event driven: https://stackoverflow.com/a/721743 – Kasper Cottaar Sep 05 '22 at 11:35
  • @LordWabbit It could work, and it does. The question is not about the "best" method, but the "simplest". This is one of the simplest methods imaginable. And this answer may not be the best, but surely is far more useful than your rocks-banging analogy, or, in fact, your whole condescending comment. – Daniel Mar 30 '23 at 10:43
3

There's also COM.

There are technicalities, but I'd say the advantage is that you'll be able to call methods that you can define.

MSDN offers C# COM interop tutorials. Please search because these links do change.

To get started rightaway go here...

Kay Zed
  • 1,304
  • 2
  • 21
  • 31
2

There's also MSMQ (Microsoft Message Queueing) which can operate across networks as well as on a local computer. Although there are better ways to communicate it's worth looking into: https://msdn.microsoft.com/en-us/library/ms711472(v=vs.85).aspx

Rowan
  • 21
  • 1
2

The easiest solution in C# for inter-process communication when security is not a concern and given your constraints (two C# processes on the same machine) is the Remoting API. Now Remoting is a legacy technology (not the same as deprecated) and not encouraged for use in new projects, but it does work well and does not require a lot of pomp and circumstance to get working.

There is an excellent article on MSDN for using the class IpcChannel from the Remoting framework (credit to Greg Beech for the find here) for setting up a simple remoting server and client.

I Would suggest trying this approach first, and then try to port your code to WCF (Windows Communication Framework). Which has several advantages (better security, cross-platform), but is necessarily more complex. Luckily MSDN has a very good article for porting code from Remoting to WCF.

If you want to dive in right away with WCF there is a great tutorial here.

cdiggins
  • 17,602
  • 7
  • 105
  • 102