轉換為異步,等待使用異步定位包

[英]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. I converted into this:

我按照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);
}

It works fine. What confuses me is that i could do it without using Task but just the method of my Dal. But that means that i must have modified my Dal method, which is something i dont want?

它工作正常。令我困惑的是,我可以不使用Task而只使用我的Dal方法。但這意味着我必須修改我的Dal方法,這是我不想要的東西?

I would appreciate if someone would explain to me in "plain" words if what i did is optimal or not and why.

如果有人能用“簡單”的話向我解釋我的所作所為是最優的還是沒有,以及為什么,我將不勝感激。

Thanks

謝謝

P.s. My dal method

附:我的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;
        }
}

1 个解决方案

#1


3  

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. You are simply running your blocking DAL method inside a seprate background thread. If you want real gain, you should modify your DAL method to use asynchronous ADO.NET, a.k.a BeginXXX and EndXXX methods. Once you do that you will get real benefit from I/O Completion Ports. No threads will ever be jeopardized during the execution of the database call.

不,那根本不是最優的。如果您無法將DAL層更改為異步,則使用async / await不會獲得太多收益。您只是在一個單獨的后台線程中運行阻塞DAL方法。如果你想獲得真正的收益,你應該修改你的DAL方法以使用異步ADO.NET,a.k.a BeginXXX和EndXXX方法。一旦這樣做,您將從I / O完成端口中獲得真正的好處。在執行數據庫調用期間,任何線程都不會受到危害。

If you cannot modify your DAL method, whether you are using JDEItemLotAvailability itm = await Task.Factory.StartNew(() => Dal.GetLotAvailabilityF41021(a, b, c)); or manual thread creation, really, you gain nothing.

如果你不能修改你的DAL方法,你是否使用JDEItemLotAvailability itm = await Task.Factory.StartNew(()=> Dal.GetLotAvailabilityF41021(a,b,c));或手動創建線程,真的,你什么也得不到。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/12/02/7259f0a756063c54d7be10f402df8a4c.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com