我有这个:
private void BtnCheckClick(object sender, EventArgs e)
{
var a = txtLot.Text;
var b = cmbMcu.SelectedItem.ToString();
var c = cmbLocn.SelectedItem.ToString();
btnCheck.BackColor = Color.Red;
var task = Task.Factory.StartNew(() =>
Dal.GetLotAvailabilityF41021(a, b, c));
task.ContinueWith(t =>
{
btnCheck.BackColor = Color.Transparent;
lblDescriptionValue.Text = t.Result.Description;
lblItemCodeValue.Text = t.Result.Code;
lblQuantityValue.Text = t.Result.AvailableQuantity.ToString();
},TaskScheduler .FromCurrentSynchronizationContext() );
LotFocus(true);
}
我按照 J. Skeet 的建议进入异步,在我的 .NET 4.0 应用程序中等待。我转换成这个:
private async void BtnCheckClick(object sender, EventArgs e)
{
var a = txtLot.Text;
var b = cmbMcu.SelectedItem.ToString();
var c = cmbLocn.SelectedItem.ToString();
btnCheck.BackColor = Color.Red;
JDEItemLotAvailability itm = await Task.Factory.StartNew(() => Dal.GetLotAvailabilityF41021(a, b, c));
btnCheck.BackColor = Color.Transparent;
lblDescriptionValue.Text = itm.Description;
lblItemCodeValue.Text = itm.Code;
lblQuantityValue.Text = itm.AvailableQuantity.ToString();
LotFocus(true);
}
它工作正常。让我感到困惑的是,我可以在不使用 Task 的情况下做到这一点,而只需使用我的 Dal 的方法。但这意味着我必须修改我的 Dal 方法,这是我不想要的?
如果有人能用“简单”的话向我解释我所做的是否是最佳的以及为什么,我将不胜感激。
谢谢
Ps我的dal方法
public bool CheckLotExistF41021(string _lot, string _mcu, string _locn)
{
using (OleDbConnection con = new OleDbConnection(this.conString))
{
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "select lilotn from proddta.f41021 " +
"where lilotn = ? and trim(limcu) = ? and lilocn= ?";
cmd.Parameters.AddWithValue("@lotn", _lot);
cmd.Parameters.AddWithValue("@mcu", _mcu);
cmd.Parameters.AddWithValue("@locn", _locn);
cmd.Connection = con;
con.Open();
OleDbDataReader rdr = cmd.ExecuteReader();
bool _retval = rdr.HasRows;
rdr.Close();
con.Close();
return _retval;
}
}