What is the simplest method of inter-process commu

2018-12-31 20:32发布

I want communicate between a parent and child process both written in C#. It should be asynchronous, event driven. I does not want run a thread in every process that handle the very rare communication.

What is the best solution for it?

6条回答
其实,你不懂
2楼-- · 2018-12-31 20:52

Anonymous pipes.

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

Use Asynchronous operations with BeginRead/BeginWrite and AsyncCallback.

查看更多
浪荡孟婆
3楼-- · 2018-12-31 20:56

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

查看更多
怪性笑人.
4楼-- · 2018-12-31 21:03

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...

查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 21:06

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);
        }
    }
    //...
}
查看更多
余欢
7楼-- · 2018-12-31 21: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.

查看更多
登录 后发表回答