【发布时间】:2018-07-03 22:45:27
【问题描述】:
我创建了以下代码。我以为它会起作用,但事实并非如此。 await 无限期地挂在那里(永远不会调用 Exit 事件)。
我在没有参数的情况下调用“ping”,所以它几乎会立即结束。
using System;
using System.Diagnostics;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static async Task Main(string[] args)
{
var process = new Process
{
StartInfo =
{
FileName = "ping",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
},
EnableRaisingEvents = true
};
var obs = Observable.FromEventPattern(handler => process.Exited += handler, handler => process.Exited -= handler);
var started = process.Start();
if (!started)
{
throw new InvalidOperationException("Could not start process: " + process);
}
await obs.FirstAsync();
}
}
}
如何使用 IObservable 使其工作?
【问题讨论】:
标签: c# .net async-await system.reactive