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);
}
}
}
}
Dear, I have a WPF list for display a list of store procedure of an Database. When the target of connection string changed, I have do query a list store procedure of this database in async task and display the result to a list.
The issue is:
Unfortunately, the result of First time is come later then Second time , so that, the list view result is overwrite, the Database is B but the list Store of Procedures is belong to Database A.
Would you please help me how to prevent this issue?
You could store the value of the connection string passed to the method in a variable and then compare the current value of the property to the value of this variable once the async method has completed.
If they differ, you just throw away the result and wait for the last async call to complete:
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);
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.