|
■No19072 (鶏唐揚 さん) に返信
.NET Framework 1.1 でもできる範囲のコードで。
class AsyncArgument
{
public Process process;
public Stream stream;
public byte[] buffer;
}
private static void Ping2()
{
using (Process p = new Process())
{
p.StartInfo.FileName = "ping.exe";
p.StartInfo.Arguments = "127.0.0.1";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.Start();
byte[] outputBuffer = new byte[1024];
AsyncArgument aa = new AsyncArgument();
aa.process = p;
aa.stream = p.StandardOutput.BaseStream;
aa.buffer = outputBuffer;
aa.stream.BeginRead(outputBuffer, 0, 1024, StandardOutputCallback, aa);
p.WaitForExit();
}
Console.WriteLine();
Console.WriteLine("Complete");
}
static void StandardOutputCallback(IAsyncResult result)
{
AsyncArgument aa = result.AsyncState as AsyncArgument;
int count = aa.stream.EndRead(result);
string output = Console.OutputEncoding.GetString(aa.buffer, 0, count);
if (!string.IsNullOrEmpty(output))
{
Console.WriteLine(output);
}
if (!aa.process.HasExited)
{
aa.stream.BeginRead(aa.buffer, 0, 1024, StandardOutputCallback, aa);
}
}
しかし出力は細切れになってしまう。
|