我正在学习async/await,在我读完这篇文章之后不要阻止异步代码
我从@Stephen Cleary的文章中注意到了一个提示.
使用ConfigureAwait(false)来避免死锁是一种危险的做法.您必须对阻塞代码调用的所有方法的传递闭包中的每个等待使用ConfigureAwait(false),包括所有第三方和第二方代码.使用ConfigureAwait(false)来避免死锁充其量只是一个黑客攻击.
它在我上面附上的帖子代码中再次出现.
public async Task<HtmlDocument> LoadPage(Uri address)
{
using (var httpResponse = await new HttpClient().GetAsync(address)
.ConfigureAwait(continueOnCapturedContext: false)) //IO-bound
using (var responseContent = httpResponse.Content)
using (var contentStream = await responseContent.ReadAsStreamAsync()
.ConfigureAwait(continueOnCapturedContext: false)) //IO-bound
return LoadHtmlDocument(contentStream); //CPU-bound
}
Run Code Online (Sandbox Code Playgroud)
据我所知,当我们使用ConfigureAwait(false)时,其余的异步方法将在线程池中运行.为什么我们需要在传递闭包中将它添加到每个等待中?我自己只是认为这是我所知道的正确版本.
public async Task<HtmlDocument> LoadPage(Uri address)
{
using (var httpResponse = await new HttpClient().GetAsync(address)
.ConfigureAwait(continueOnCapturedContext: false)) //IO-bound
using (var responseContent = httpResponse.Content)
using (var contentStream = await responseContent.ReadAsStreamAsync()) //IO-bound
return LoadHtmlDocument(contentStream); //CPU-bound
}
Run Code Online (Sandbox Code Playgroud)
这意味着在使用块中第二次使用ConfigureAwait(false)是没用的.请告诉我正确的方法.提前致谢.
我计划将数据访问层迁移到使用存储库模式和工作单元.
我知道存储库将帮助我轻松地将持久性存储(数据库,集合等)和EF等技术更改为MongoDB.所以我注意到了一些存储库实现的关键点,例如:
IEnumerable而不是IQueryable如果我在项目中的实现存储库中应用这些关键点,我完全忘记了如何处理与多个实体相关的复杂查询.
目前,我已经有了是对BLL库有很多服务类将直接联系到DbContext与DbSetEF和一些像这样的验证:
public IEnumerable<ProjectDTO> GetProjectWithDetails()
{
// Validation
// Logging
// Can be any logic need to before query data.
Dbcontext.Projects.Where(p =>
// multiple of conditions go here follow business rules
// conditions will need to check another entities (task, phase, employee...) such as:
// 1. project have task status 'in-progress' .. etc
// 2. project have employeeid 1,2,3..
// 3. project have stask start at some …Run Code Online (Sandbox Code Playgroud) 我对 winforms 中的消息循环如何工作感到非常困惑。我正在处理 Windows 表单,我知道何时调用Application.Run(myform),因此它将创建一个消息队列和消息循环,然后显示我的表单并开始在消息队列中检索消息以进行调度。
我阅读了一些主题,但仍然不明白消息循环在幕后如何工作,UI 线程同时运行消息泵和执行代码?以及消息循环如何使用线程?
我的问题是:消息循环是否会在 UI 线程上运行。如果是,为什么它不阻塞 UI 线程?
如果问题不清楚,请告诉我,对不起,因为我的英语不好。
我正在学习单页应用程序并在阅读文档本身之后
我想知道单页应用程序模式只是使用外部模板剔除的 Web 应用程序中的一页(例如:html 页面)?
我的意思是(我正在使用 MVC ):
-mywebsite
+ some js files
+ some css files
+ index.html
+ controllers
+ models
Run Code Online (Sandbox Code Playgroud)
我希望有人可以为我解释更多有关此模式的信息。谢谢。