包的同步方法为可以'等待'在一个异步1 [英] Wrap a synchronous method into an async one which can be 'waited' upon
本文介绍了包的同步方法为可以'等待'在一个异步1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个同步调用:
_context.User.Where((u) => (u.UserID == twitterId && u.Type == UserType.Show)).SingleOrDefault();
这是我需要包装成一个异步其中一个我可以等上使用等待
关键字。
That I need to wrap into an async one which I can wait on using the await
keyword.
我怎样才能做到这一点?
How can I achieve this?
感谢。
推荐答案
您需要使用包装您的同步调用 Task.Run
方法。
You need to wrap your sync call using Task.Run
method.
var user = await Task.Run(() =>
_context.User
.Where(u => u.UserID == twitterId && u.Type == UserType.Show)
.SingleOrDefault());
请记住,在的EntityFramework 6.0版将具有异步接口,让您不再需要使用这种code。
Keep in mind that EntityFramework in version 6.0 will have async interfaces, so you would no longer need to use this code.
这篇关于包的同步方法为可以'等待'在一个异步1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文