值丢失和调试代码时跳过

时间:2012-04-30 23:37:16

标签: c# asp.net

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

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的警告说“无法进行步骤。进程未同步”。我面临的实际错误是字典中的值丢失或未初始化。我无法分辨,因为我无法单步执行代码。

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

0 个答案:

没有答案