What is the simplest method of inter-process communication between 2 C# processes?

后端 未结 7 1553
甜味超标
甜味超标 2020-11-22 07:31

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 thre

相关标签:
7条回答
  • 2020-11-22 08:10

    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.

    0 讨论(0)
  • 2020-11-22 08:14

    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.

    0 讨论(0)
  • 2020-11-22 08:15

    Anonymous pipes.

    Use Asynchronous operations with BeginRead/BeginWrite and AsyncCallback.

    0 讨论(0)
  • 2020-11-22 08:19

    Named pipes on WCF.

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

    0 讨论(0)
  • 2020-11-22 08:21

    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

    0 讨论(0)
  • 2020-11-22 08:25

    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);
            }
        }
        //...
    }
    
    0 讨论(0)
提交回复
热议问题