简体   繁体   English

转换为异步,等待使用异步定位包

[英]Converting to async,await using async targeting package

I have this: 我有这个:

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);
}

and i followed J. Skeet's advice to move into async,await in my .NET 4.0 app. 并且我遵循了J. Skeet的建议,进入了我的.NET 4.0应用程序中的异步等待状态。 I converted into this: 我转换成这个:

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);
}

It works fine. 工作正常。 What confuses me is that i could do it without using Task but just the method of my Dal. 令我感到困惑的是,我可以不使用Task而是仅使用Dal的方法来做到这一点。 But that means that i must have modified my Dal method, which is something i dont want? 但这意味着我必须修改我的Dal方法,这是我不想要的?

I would appreciate if someone would explain to me in "plain" words if what i did is optimal or not and why. 如果有人会以“简单”的字眼向我解释我的所作所为是否是最佳选择以及为什么最佳选择,我将不胜感激。

Thanks 谢谢

Ps My dal method Ps我的达尔法

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;
        }
}

No, that's not optimal at all. 不,这根本不是最佳选择。 If you cannot change your DAL layer to be asynchronous you are not gaining much by using async/await. 如果您不能将DAL层更改为异步层,则使用async / await不会带来太多好处。 You are simply running your blocking DAL method inside a seprate background thread. 您只是在单独的后台线程中运行阻塞的DAL方法。 If you want real gain, you should modify your DAL method to use asynchronous ADO.NET, aka BeginXXX and EndXXX methods. 如果您想获得真正的收益,则应修改DAL方法以使用异步ADO.NET,也称为BeginXXX和EndXXX方法。 Once you do that you will get real benefit from I/O Completion Ports. 一旦这样做,您将从I / O完成端口中真正受益。 No threads will ever be jeopardized during the execution of the database call. 在执行数据库调用期间不会危及任何线程。

If you cannot modify your DAL method, whether you are using JDEItemLotAvailability itm = await Task.Factory.StartNew(() => Dal.GetLotAvailabilityF41021(a, b, c)); 如果您不能修改DAL方法,则是否使用JDEItemLotAvailability itm = await Task.Factory.StartNew(() => Dal.GetLotAvailabilityF41021(a, b, c)); or manual thread creation, really, you gain nothing. 或手动创建线程,实际上,您一无所获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM