Car*_*rra 39
匿名管道.
http://msdn.microsoft.com/en-us/library/bb546102.aspx
使用与BeginRead/BeginWrite和AsyncCallback的异步操作.
小智 16
如果你的进程在同一台计算机上,你可以简单地使用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();
}
Run Code Online (Sandbox Code Playgroud)
检测Main上的args
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);
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
我建议使用Windows Communication Foundation:
http://en.wikipedia.org/wiki/Windows_Communication_Foundation
您可以来回传递对象,使用各种不同的协议.我建议使用二进制tcp协议.
以下是异步进行进程间通信的方法: