C# 的 Rx 中无法等待 system.IObservable.*** 错误
Cannot await system.IObservable.*** error in Rx of C#
问题很简单。我从网站 http://rxwiki.wikidot.com/101samples#toc0 此处找到这段代码。
public static async void StartBackgroundWork()
{
Console.WriteLine("Shows use of Start to start on a background thread:");
var o = Observable.Start(() =>
{
//This starts on a background thread.
Console.WriteLine("From background thread. Does not block main thread.");
Console.WriteLine("Calculating...");
Thread.Sleep(1000);
Console.WriteLine("Background work completed.");
});
**await o.FirstAsync();** // subscribe and wait for completion of background operation. If you remove await, the main thread will complete first.
Console.WriteLine("Main thread completed.");
}
但是当我将它粘贴到我的项目中时,出现一个错误 "Cannot await system.IObservable "。它出什么问题了?我怎样才能运行这个代码?鉴于我对 Rx 和 C# 不是很熟悉,如果问题很愚蠢,请原谅我。
您可能正在链接到 .NET 4.0 而不是 .NET 4.5 的 Rx DLL。
如果您从 Nuget 获取包,请尝试使用 Nuget 控制台中的命令 "Update-Package –reinstall" 重新安装包。有关详细信息,请参阅:http://docs.nuget.org/consume/reinstalling-packages
您可以前往项目的 Reference/System.Reactive.Core 的 属性 选项卡查看当前使用的版本,并查看 "Path" 属性.它应该包含 "net45" 如下:
+++\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll
问题很简单。我从网站 http://rxwiki.wikidot.com/101samples#toc0 此处找到这段代码。
public static async void StartBackgroundWork()
{
Console.WriteLine("Shows use of Start to start on a background thread:");
var o = Observable.Start(() =>
{
//This starts on a background thread.
Console.WriteLine("From background thread. Does not block main thread.");
Console.WriteLine("Calculating...");
Thread.Sleep(1000);
Console.WriteLine("Background work completed.");
});
**await o.FirstAsync();** // subscribe and wait for completion of background operation. If you remove await, the main thread will complete first.
Console.WriteLine("Main thread completed.");
}
但是当我将它粘贴到我的项目中时,出现一个错误 "Cannot await system.IObservable "。它出什么问题了?我怎样才能运行这个代码?鉴于我对 Rx 和 C# 不是很熟悉,如果问题很愚蠢,请原谅我。
您可能正在链接到 .NET 4.0 而不是 .NET 4.5 的 Rx DLL。
如果您从 Nuget 获取包,请尝试使用 Nuget 控制台中的命令 "Update-Package –reinstall" 重新安装包。有关详细信息,请参阅:http://docs.nuget.org/consume/reinstalling-packages
您可以前往项目的 Reference/System.Reactive.Core 的 属性 选项卡查看当前使用的版本,并查看 "Path" 属性.它应该包含 "net45" 如下:
+++\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll