private async void ExternalConnectionStringVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if(e.PropertyName == "ConnectionString")
{
if (this.ExternalConnectionStringVM.CanConnect)
{
Services.SqlServerDatabaseInfoService service = new Services.SqlServerDatabaseInfoService();
var sps = await service.GetAllStoreProceduresAsync(this.ExternalConnectionStringVM.ConnectionStringModel.ConnectionString);
this.ExternalStoreProcedures.Clear();
foreach (string sp in sps.Result)
{
this.ExternalStoreProcedures.Add(sp);
}
}
}
}
亲爱的,我有一个WPF列表,用于显示数据库的存储过程列表。当连接字符串的目标发生变化时,我在异步任务中查询该数据库的列表存储过程,并将结果显示在列表中。
问题是:
不幸的是,第一次的结果是晚于第二次,因此,列表视图结果被覆盖,数据库是B但是程序存储列表属于数据库A.
你能帮我解决一下这个问题吗?
您可以将传递给方法的连接字符串的值存储在变量中,然后在异步方法完成后将属性的当前值与此变量的值进行比较。
如果它们不同,您只需丢弃结果并等待最后一次异步调用完成:
private async void ExternalConnectionStringVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "ConnectionString")
{
if (this.ExternalConnectionStringVM.CanConnect)
{
Services.SqlServerDatabaseInfoService service = new Services.SqlServerDatabaseInfoService();
//copy the string into a variable
string connectionStringBefore = this.ExternalConnectionStringVM.ConnectionStringModel.ConnectionString;
//call the method
var sps = await service.GetAllStoreProceduresAsync(connectionString);
//compare the string with the current property
if (this.ExternalConnectionStringVM.ConnectionStringModel.ConnectionString == connectionStringBefore)
{
this.ExternalStoreProcedures.Clear();
foreach (string sp in sps.Result)
{
this.ExternalStoreProcedures.Add(sp);
}
}
}
}
}