C# 值丢失,而调试代码只是跳过

C# 值丢失,而调试代码只是跳过,c#,asp.net,C#,Asp.net,我很确定这是因为我在代码中犯了一个错误,但看不出是什么错误。我试图在启动时将数据加载到静态列表中,并在一定时间间隔后刷新此数据。为此,我创建了一个类,其中我创建了两个实例,一个作为主实例,另一个作为辅助实例 public class DataAndPartitions { public DateTime LastUpdated {get;set;} public List<AppUser> LatestUsers {get;set;} public Dict

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

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
    }
}
公共类数据和分区
{
公共日期时间上次更新{get;set;}
公共列表迟到者{get;set;}
公共词典LatestUsersByCategory{get;set;}
公共数据和分区()
{
this.LastUpdated=DateTime.Now;
this.LatestUsers=新列表();
this.LatestUsersByCategory=新字典();
this.LatestUsersByCategory=InitLatestUsersByCategory(this.LatestUsersByCategory);
}
静态字典InitLatestUsersByCategory(字典到字典)
{
//初始化字典的代码
}
}
现在在另一节课上

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();
        }
    }
}
公共类AppSearch
{
静态列表dataAndPartitions=新列表();
静态void InitDataAndPartitions()
{
if(dataAndPartitions.Count==0)
{
添加(新的dataAndPartitions());
睡眠(100);
添加(新的dataAndPartitions());
}
}
静态列表RecentAppUsers
{
得到
{
if(dataAndPartitions.Count==0)
{
InitDataAndPartitions();
}
//比较两个dataAndPartitions的LastUpdated并返回mostRecentlyUpdatedIndex
int mostrecentlyupdatedinex=getmostrecentlyupdatedinex();
返回dataAndPartitions[mostRecentlyUpdatedIndex].LatestUsers;
}
}
静态字典用户SpartionedByCategory
{
得到
{
if(dataAndPartitions.Count==0)
{
InitDataAndPartitions();
}
//比较两个dataAndPartitions的LastUpdated并返回mostRecentlyUpdatedIndex
int mostrecentlyupdatedinex=getmostrecentlyupdatedinex();
返回dataAndPartitions[mostRecentlyUpdatedIndex].LatestUsersByCategory;
}
} 
静态私有void AsyncQueryCallback(IAsyncResult结果)
{
尝试
{
//为了避免管理锁定等,只需添加到新列表并更改引用即可
列表用户=RecentAppUsers;
字典usersPartition=UsersPartitionedByCategory;
SqlCommand cmd=(SqlCommand)result.AsyncState;
SqlDataReader=cmd.EndExecuteReader(结果);
while(reader.Read())
{
AppUser=新的AppUser();
user.ReadDetails(reader);
用户。添加(用户);
usersPartition[user.Category.Id]。添加(用户);
}
if(cmd.Connection.State.Equals(ConnectionState.Open))
{
listAsyncFinishedLoading=true;
cmd.Connection.Close();
}
}
捕获(例外情况除外)
{
//日志异常
}
最后
{
//即使异步加载失败,也要启动计时器以便稍后重新加载
InitReloadUsersTimer();
}
}
}
异步调用发生在Global.asax的应用程序启动时。我面临的问题是,当AsyncQueryCallBack正在执行时,有时它从读取器读取一行,将用户对象添加到必需的集合中,然后停止执行。它不会抛出异常,只会停止

我在VisualStudio2008中逐步浏览代码时观察到了这一切。为什么会发生这种情况?我的代码有什么问题

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

有几次,我收到Visual Studio发出的警报,说“无法执行步骤。进程未同步”。我面临的实际错误是字典中的值丢失或未初始化。我无法说出,因为我无法单步执行代码


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

是否已将调试器设置为显示所有CLR异常?不仅仅是代码中的异常?是的,当我转到Visual Studio菜单中的“调试->异常”时,会检查所有内容是否存在CLR异常。您是否可以在
应用程序\u Start
中发布代码?