我有一个具有以下结构的交叉线程操作。
异常是当我在记录查询操作上放置断点时,记录不为空。但是,当我删除断点并让程序执行时,记录始终为null。我确保记录检索过程是正确的。这是某种跨线程操作异常吗?
这是调用程序
Invoke(new Action(() =>
{
var filters = new KeyValuePair<string, object>[]
{
new KeyValuePair<string, object>("from_userid", ParentMessage.from_userid),
new KeyValuePair<string, object>("to_userid", this.SourceUser.id),
new KeyValuePair<string, object>("group_header_id", ParentMessage.group_header_id),
new KeyValuePair<string, object>("message", ParentMessage.message),
new KeyValuePair<string, object>("is_read", 0)
};
MessagerLogModel MessageLog = null;
MessageLog = MessagerLogs.CustomFilter(filters).FirstOrDefault();
if (MessageLog == null)
{
MessageBox.Show("message log model not found in database.");
return;
}
}));
并且,这是检索数据库记录的操作。
public static List<MessagerLogModel> CustomFilter(KeyValuePair<string, object>[] _params)
{
var result = new List<MessagerLogModel>();
string Query = @"SELECT * FROM messager_logs";
List<MySqlParameter> commandParams = new List<MySqlParameter>();
foreach (KeyValuePair<string, object> param in _params)
{
if (commandParams.Count == 0)
Query += " WHERE ";
else
Query += " AND ";
if (param.Value == DBNull.Value)
Query += param.Key + " IS @" + param.Key;
else
Query += param.Key + " = @" + param.Key;
commandParams.Add(new MySqlParameter("@" + param.Key, param.Value));
}
MySqlDataReader r =
MySqlHelper.ExecuteReader(Shared.getConnectionString(),
Query, commandParams.ToArray());
while (r.Read())
result.Add(dataToModel(r));
r.Close();
return result;
}
答案 0 :(得分:3)
问题是&#34; lambda&#34;在你的行动中。在那里包含一些值:在定义lambda时,它们不会被复制到lambda表达式中,但是在执行lambda时。像ParentMessage.from_userid这样的成员变量可能同时发生了变化。