相关疑难解决方法(0)

为什么尝试/最终而不是"使用"语句有助于避免竞争条件?

此问题涉及另一个帖子中的评论:取消实体框架查询

为清楚起见,我将从那里重现代码示例:

    var thread = new Thread((param) =>
    {
        var currentString = param as string;

        if (currentString == null)
        {
            // TODO OMG exception
            throw new Exception();
        }

        AdventureWorks2008R2Entities entities = null;
        try // Don't use using because it can cause race condition
        {
            entities = new AdventureWorks2008R2Entities();

            ObjectQuery<Person> query = entities.People
                .Include("Password")
                .Include("PersonPhone")
                .Include("EmailAddress")
                .Include("BusinessEntity")
                .Include("BusinessEntityContact");
            // Improves performance of readonly query where
            // objects do not have to be tracked by context
            // Edit: But it doesn't …
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous entity-framework race-condition

24
推荐指数
2
解决办法
1999
查看次数