これはコードの間違いによるものだと確信していますが、何が間違いなのかわかりません。起動時にデータを静的リストにロードし、一定の間隔でこのデータを更新しようとしています。このために、2 つのインスタンスを作成するクラスを作成しました。1 つはプライマリとして機能し、もう 1 つはセカンダリとして機能します。
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 が実行されているときに、リーダーから 1 行を読み取り、必要なコレクションにユーザー オブジェクトを追加してから、実行を停止することがあります。例外はスローされませんが、停止するだけです。
これらはすべて、Visual Studio 2008 でコードをステップ実行しているときに観察したものです。なぜこれが起こるのでしょうか? コードの何が問題になっていますか?
==編集== これは私がさらに気づいたことであり、質問のヘッダーを変更しました。
Visual Studio から「ステップ実行できません。プロセスが同期されていません」というアラートを数回受け取りました。私が直面している実際のバグは、ディクショナリの値が失われたり、初期化されなかったりすることです。コードをステップ実行できないため、わかりません。
興味深いのは、実行がスキップされる前に数回 while Reader.read ループをステップスルーできたにもかかわらず、Dictionary にあるList<AppUser> LatestUsers
それぞれがカウント = 0 であるすべての値を持っていることです。値がディクショナリに追加されていることをウォッチ ウィンドウで確認できました。しかしその後、それらは失われました。なぜこうなった?List<AppUser>
LatestUsersByCategory
List<AppUser>