SQL 2查询(由数据绑定调用)完全冻结WPF应用程序 [英] SQL 2 LINQ query (called by databinding) completely freezing WPF application

查看:138
本文介绍了SQL 2查询(由数据绑定调用)完全冻结WPF应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早些时候,我正在追捕一个非常奇怪的错误,我终于把它从
追溯到似乎是造成这个问题。

Earlier today I was hunting down a very weird bug... I finally traced it down to what seems to be causing the problem.

原始报告可以在这里找到:原始问题

The original report can be found here: original question

细节发生了变化,足以保证提出新的问题。

The details have changed enough to warrant a new question.

有时候,这似乎是我的应用程序所有的时间,当它达到以下LINQ查询时冻结

It would seem my application sometimes, NOT ALL OF THE TIME, freezes when it reaches the following LINQ query:

using (NetworkDatabaseContext db = new 

NetworkDatabaseContext(UISession.ConnectionString))
{
   Ballast ballast = db.Ballasts.FirstOrDefault(b => b.NetworkId == UISession.NetworkId &&
          b.ShortAddress == this.innerBallast.ShortAddress && b.ControllerSerial == this.controllerSerial);

这是它的样子:

不回应http://www.freeimagehosting.net/uploads/1c2c921835.jpg

大部分时间这个工作很好,但是每一次,它都会锁定。
此代码是BallastListItem类的一部分。这个类的项目绑定到页面上的ListBox:

Most of the time this works just fine... but every now and then it will lock up. This code is part of a BallastListItem class. Items of this class are bound to a ListBox on the Page:

<ListView Name="lstBallasts" Margin="5" DockPanel.Dock="Top" MinHeight="100"><!-- The MinHeight is used to get a good view in the designer -->
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Address" DisplayMemberBinding="{Binding InnerBallast.ShortAddress}" Width="70"/>
                <GridViewColumn Header="Name" Width="300">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Name="txtBallastDisplayName" Text="{Binding DisplayName}" Width="270" MaxWidth="270" MaxLength="100"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Type" DisplayMemberBinding="{Binding DeviceType}" Width="150"/>
                <GridViewColumn Header="Status" DisplayMemberBinding="{Binding InnerBallast.StandardVersion}" Width="150"/>
            </GridView>
        </ListView.View>
    </ListView> 

代码是DisplayName属性getter的一部分:

The code is part of the DisplayName property getter:

public string DisplayName
    {
        get
        {
            using (NetworkDatabaseContext db = new NetworkDatabaseContext(UISession.ConnectionString))
            {
                Ballast ballast = db.Ballasts.FirstOrDefault(b => b.NetworkId == UISession.NetworkId && 
                   b.ShortAddress == this.innerBallast.ShortAddress && b.ControllerSerial == this.controllerSerial);

所以这个代码是在数据绑定发生时被调用的,应该已经意识到了。
仍然我不知道为什么这有时会导致问题...

So this code is called when databinding occurs, should have realized that before. Still I have no idea why this would sometimes cause problems...

更新

在这两种情况下(当应用程序冻结时,如果没有)
连接状态在查询代码之前是已关闭,正如我可以看到的:

In both cases (when the application freezes and when it does not) the state of the connection is "Closed" just before the query code, as I can see from:

db.Connection.State.ToString()

更新2
我忘了提到我把该代码移出了getter,
,因此它只被提取一次。在第一次实施
时必须一直在睡觉。代码工作正常,虽然在XP ...每个现在
然后,但更少的问题仍然发生。

UPDATE 2 I forgot to mention I moved that code out of the getter, so it is only fetched once. Must have been sleeping while implementing this the first time. The code works fine now, although on XP... every now and then, but much less often the problem still occurs.

*更新3 *
为了清楚起见,我使用的SQL压缩数据库很少数据

* UPDATE 3 * Just to be clear, I'm using an SQL Compact database with very little data

推荐答案

具有打开数据库连接并运行查询的属性不是一个好的模式。

Having a property open a database connection and run a query is not a good pattern.

更好的方法是从LINQ到SQL查询一组对象,并将其绑定到WPF控件。

A better approach would be to query a set of objects from LINQ to SQL and bind those to the WPF control instead.

这篇关于SQL 2查询(由数据绑定调用)完全冻结WPF应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆