2 个 C# 进程之间进行进程间通信的最简单方法是什么?

发布于 2024-07-13 21:44:31 字数 118 浏览 2 评论 0原文

我想在父进程和子进程之间创建通信,两者都是用 C# 编写的。

它应该是异步的、事件驱动的。

我不想在每个进程中运行一个线程来处理非常罕见的通信。

最好的解决方案是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

━╋う一瞬間旳綻放 2024-07-20 21:44:31

匿名管道

将异步操作与 BeginRead/BeginWrite 和 AsyncCallback 结合使用。

Anonymous pipes.

Use Asynchronous operations with BeginRead/BeginWrite and AsyncCallback.

飘落散花 2024-07-20 21:44:31

如果您的进程在同一台计算机上,则只需使用stdio即可。

这是我的用法,网页屏幕截图:

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);
        }
    }
    //...
}

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);
        }
    }
    //...
}
迷爱 2024-07-20 21:44:31

我建议使用 Windows Communication Foundation:

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

您可以来回传递对象,使用各种不同的协议。 我建议使用二进制 tcp 协议。

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.

小忆控 2024-07-20 21:44:31

当不考虑安全性并且考虑到您的限制(同一台计算机上的两个 C# 进程)时,C# 中用于进程间通信的最简单解决方案是 Remoting API。 现在,远程处理是一种遗留技术(与已弃用的技术不同),不鼓励在新项目中使用,但它确实运行良好,并且不需要太多的盛况和环境即可发挥作用。

有一个 MSDN 上有关使用远程处理框架中的类 IpcChannel 的优秀文章(归功于 Greg Beech 在此处找到)用于设置简单的远程处理服务器和客户端。

我建议首先尝试这种方法,然后尝试将代码移植到 WCF(Windows 通信框架)。 它有几个优点(更好的安全性、跨平台),但必然更复杂。 幸运的是,MSDN 有一个非常好的有关将代码从 Remoting 移植到 WCF 的文章

如果您想立即深入了解 WCF,这里有一个很棒的教程

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.

清眉祭 2024-07-20 21:44:31

还有 COM

有一些技术细节,但我想说优点是您将能够调用您可以定义的方法。

MSDN 提供了 C# COM 互操作教程。 请搜索,因为这些链接确实发生了变化。

要立即开始,请转到此处 ...

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

往事随风而去 2024-07-20 21:44:31

如果可能的话,制作共享文件会更容易!

//out
File.AppendAllText("sharedFile.txt", "payload text here");
// in
File.ReadAllText("sharedFile.txt");

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

//out
File.AppendAllText("sharedFile.txt", "payload text here");
// in
File.ReadAllText("sharedFile.txt");
口干舌燥 2024-07-20 21:44:31

还有 MSMQ(Microsoft 消息队列),它可以跨网络以及在本地计算机上运行。 尽管有更好的沟通方式,但值得研究一下: https://msdn.microsoft.com/en-us/library/ms711472(v=vs.85).aspx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文