0

我很确定这是由于我在代码中犯了一个错误,但看不出是什么错误。我试图在启动时将数据加载到静态列表中,并在一定间隔后刷新此数据。为此,我创建了一个类,我创建了两个实例,一个作为主要实例,一个作为次要实例。

public class DataAndPartitions
{
    public DateTime LastUpdated {get;set;}
    public List<AppUser>  LatestUsers {get;set;}
    public Dictionary<int, List<AppUser>> LatestUsersByCategory {get; set;}

    public DataAndParitions()
    {
        this.LastUpdated = DateTime.Now;
        this.LatestUsers = new List<AppUser>();
        this.LatestUsersByCategory = new Dictionary<int,List<AppUser>>();

        this.LatestUsersByCategory = InitLatestUsersByCategory(this.LatestUsersByCategory);
    }

    static Dictionary<int,List<AppUser>> InitLatestUsersByCategory(Dictionary<int,List<AppUser>> toInitDictionary)
    {
        //Code to Initialize the dictionary
    }
}

现在在其他班级

public class AppSearch
{
    static List<DataAndPartitions> dataAndPartitions = new List<DataAndPartitions>();

    static void InitDataAndPartitions()
    {
        if (dataAndPartitions.Count == 0)
        {
            dataAndPartitions.Add(new DataAndPartitions());
            Thread.Sleep(100);
            dataAndPartitions.Add(new DataAndPartitions());
        }
    }

    static List<AppUser> RecentAppUsers
    {
        get
        {

            if (dataAndPartitions.Count == 0)
            {
                InitDataAndPartitions();
            }

            //compares the LastUpdated of the two dataAndPartitions and returns mostRecentlyUpdatedIndex
            int mostRecentlyUpdatedIndex = GetMostRecentlyUpdatedIndex();

            return dataAndPartitions[mostRecentlyUpdatedIndex].LatestUsers;                                
        }
    }

    static Dictionary<int, List<AppUser>> UsersPartitionedByCategory
    {
        get 
        {
            if (dataAndPartitions.Count == 0)
            {
                InitDataAndPartitions();
            }

            //compares the LastUpdated of the two dataAndPartitions and returns mostRecentlyUpdatedIndex
            int mostRecentlyUpdatedIndex = GetMostRecentlyUpdatedIndex();

            return dataAndPartitions[mostRecentlyUpdatedIndex].LatestUsersByCategory;               
        }
    } 

    static private void AsyncQueryCallback(IAsyncResult result)
    {
        try
        {
            //To avoid managing locking etc., just add to a new List and change reference
            List<AppUser> users = RecentAppUsers;
            Dictionary<int, List<AppUser>> usersPartition = UsersPartitionedByCategory;

            SqlCommand cmd = (SqlCommand)result.AsyncState;
            SqlDataReader reader = cmd.EndExecuteReader(result);
            while (reader.Read())
            {
                AppUser user = new AppUser();
                user.ReadDetails(reader);
                users.Add(user);
                usersPartition[user.Category.Id].Add(user);
            }

            if (cmd.Connection.State.Equals(ConnectionState.Open))
            {
                listAsyncFinishedLoading = true;
                cmd.Connection.Close();
            }
        }
        catch (Exception ex)
        {
        //Logs Exception
        }
        finally 
        {
                //Even if the async load fails, start the timer to reload at a later time
                InitReloadUsersTimer();
        }
    }
}

异步调用发生在 Global.asax 的 Application_Start 处。我面临的问题是,当 AsyncQueryCallBack 正在执行时,有时它会从读取器读取一行,将用户对象添加到必要的集合中,然后停止执行。它不会抛出异常,只是停止。

我在单步执行 Visual Studio 2008 中的代码时观察到的所有这些情况。为什么会发生这种情况?我的代码有什么问题?

==编辑== 这是我进一步注意到的,因此更改了问题标题。

有几次,我收到了来自 Visual Studio 的警报,提示“无法步进。进程未同步”。我面临的实际错误是 Dictionary 中的值丢失或未初始化。我无法判断,因为我无法单步执行代码。

有趣的是,它List<AppUser> LatestUsers具有所有值,因为List<AppUser>字典中的每个值都LatestUsersByCategory具有 count = 0,即使有几次我能够在执行跳过之前几次单步执行 while reader.read 循环,我能够在手表窗口中看到值被添加到List<AppUser>字典中。但后来他们迷路了。为什么会这样?

4

0 回答 0