有
found this way使用等待通过回调调用异步方法,我在Argotic RSS阅读器库上使用它:
var tcs = new taskcompletionsource<string>(); EventHandler<SyndicationResourceLoadedEventArgs> FeedReaderOnLoaded = null; FeedReaderOnLoaded = (sender,args) => { FeedReader.Loaded -= FeedReaderOnLoaded; tcs.SetResult(""); // Needed so the await completes }; FeedReader.Loaded += FeedReaderOnLoaded; FeedReader.LoadAsync(new Uri(Feed.Url),new object()); await tcs.Task; // Result is put in a property,rather than returned from the method var items = FeedReader.Channel.Items;
所以这很好用,我得到了我的物品.
我注意到在fiddler中,两个调用正在对RSS Feed的url进行调用.单步执行调试器中的代码后,会在LoadAsync上调用一次,并再次等待tcs.Task.我应该怎么做才能消除其中一个电话?
更新有一个控制台应用程序项目,可以是downloaded from here,演示此行为.
更新我已经改变了我使用Argotic库的方式,因此我使用HttpClient下载RSS提要,然后将内容作为字符串传递给Argotic,现在只生成一个调用.如果有人有任何想法,我仍然想知道为什么它会被召唤两次.
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。