【发布时间】:2016-07-20 22:11:36
【问题描述】:
我有以下方法
public List<VatRate> GetAll( string cnString )
{
List<VatRate> result = new List<VatRate>();
using (SqlConnection cn = new SqlConnection(cnString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = SQL_SELECT;
cmd.CommandType = System.Data.CommandType.Text;
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
VatRate vr = new VatRate();
vr.IDVatRate = reader["IDVatRate"] == System.DBNull.Value ? Guid.Empty : (Guid)reader["IDVatRate"];
vr.Percent = reader["Percent"].XxNullDecimal();
vr.Type = reader["Type"].XxNullString();
vr.VatRateDescription = reader["VatRateDescription"].XxNullString();
}
}
reader.Close();
cn.Close();
}
return result;
}
它将在 WPF 应用程序中使用,我希望能够通知 UI 阅读进度。我必须提出一个简单的事件吗?就像是 OnProgress(new MyProgressEventHandler(recordcounter));我肯定知道这样的方法会在执行时冻结 UI,有没有更好的方法可以做,例如使用异步方法仍然等待方法执行但能够通知用户它在做什么?
【问题讨论】:
-
您应该可以调用
async方法,然后只需调用await。 -
也许,一个带有 BackgroundWorker 的 Progressbar 的想法可能是参考之一..
-
异步等待似乎是一个不错的方法,但它不能解决我的进度通知问题,我是否必须设置异步等待并使用事件来通知 UI?在这种情况下是否存在交叉线程问题?提前谢谢你
标签: c# wpf sqldatareader