80

どちらも C# で記述された親プロセスと子プロセスの間の通信を作成したいと考えています。

非同期で、イベント駆動型である必要があります。

非常にまれな通信を処理するために、すべてのプロセスでスレッドを実行したくありません。

それに対する最善の解決策は何ですか?

4

9 に答える 9

51

匿名パイプ

BeginRead/BeginWrite および AsyncCallback で非同期操作を使用します。

于 2009-02-09T15:48:08.533 に答える
21

プロセスが同じコンピューター内にある場合は、単に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);
        }
    }
    //...
}
于 2014-04-28T05:13:30.950 に答える
9

Windows Communication Foundation を使用することをお勧めします。

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

オブジェクトをやり取りしたり、さまざまな異なるプロトコルを使用したりできます。バイナリ tcp プロトコルを使用することをお勧めします。

于 2009-02-09T15:48:17.970 に答える
7

WCF の名前付きパイプ。

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

于 2009-02-09T15:49:18.607 に答える
1

以下は、プロセス間通信を非同期で行う方法です。

  1. 匿名パイプhttps://docs.microsoft.com/en-us/dotnet/api/system.io.pipes.anonymouspipeserverstream?view=net-5.0
  2. 名前付きパイプhttps://www.codeproject.com/Articles/1179195/Full-Duplex-Asynchronous-Read-Write-with-Named-Pip
  3. ソケットhttps://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example
  4. 共有メモリ
于 2021-01-15T07:21:39.653 に答える