programing

2 개의 C # 프로세스 간 프로세스 간 통신의 가장 간단한 방법은 무엇입니까?

procenter 2021. 1. 15. 19:44
반응형

2 개의 C # 프로세스 간 프로세스 간 통신의 가장 간단한 방법은 무엇입니까?


C #으로 작성된 부모와 자식 프로세스 간의 통신을 원합니다. 비동기식 이벤트 기반이어야합니다. 매우 드문 통신을 처리하는 모든 프로세스에서 스레드를 실행하고 싶지는 않습니다.

이를위한 최상의 솔루션은 무엇입니까?


익명 파이프 .

BeginRead / BeginWrite 및 AsyncCallback과 함께 비동기 작업을 사용합니다.


동일한 컴퓨터에있는 프로세스라면 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();
}

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

Windows Communication Foundation을 사용하는 것이 좋습니다.

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

객체를 앞뒤로 전달하고 다양한 프로토콜을 사용할 수 있습니다. 바이너리 tcp 프로토콜을 사용하는 것이 좋습니다.


WCF의 명명 된 파이프.

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


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


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


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.

ReferenceURL : https://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro

반응형