第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何進(jìn)行多個(gè)異步調(diào)用并從找到匹配記錄的第一次調(diào)用返回?cái)?shù)據(jù)然后停止剩余的異步調(diào)用

如何進(jìn)行多個(gè)異步調(diào)用并從找到匹配記錄的第一次調(diào)用返回?cái)?shù)據(jù)然后停止剩余的異步調(diào)用

C#
翻翻過去那場雪 2023-05-14 16:14:08
我需要通過遍歷連接字符串來進(jìn)行多個(gè)數(shù)據(jù)庫調(diào)用。數(shù)據(jù)庫中只有 1 條匹配記錄,如果我找到匹配記錄,那么我可以返回?cái)?shù)據(jù)并取消其他異步調(diào)用。using (var Contexts = instContextfactory.GetContextList()){    foreach(var context in Contexts.GetContextList())    {            // how do I make all the calls and return data from the first call that finds data and continue with further process.(don't care about other calls if any single call finds data.                   context.Insurance.GetInsuranceByANI(ani);    }}通過 ANI 獲取保險(xiǎn)public Task<IEnumerable<Insurance>> GetInsuranceByANI(string ani){    using (ITransaction transaction = Session.Value.BeginTransaction())    {        transaction.Rollback();        IDbCommand command = new SqlCommand();        command.Connection = Session.Value.Connection;        transaction.Enlist(command);        string storedProcName = "spGetInsurance";        command.CommandText = storedProcName;        command.Parameters.Add(new SqlParameter("@ANI", SqlDbType.Char, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, ani));        var rdr = command.ExecuteReader();        return Task.FromResult(MapInsurance(rdr));    }}例如:我正在循環(huán)使用 5(a, b, c, d, e) 個(gè)不同的數(shù)據(jù)庫連接字符串。我需要對所有 5 個(gè)數(shù)據(jù)庫進(jìn)行異步調(diào)用。如果我在 db : b 中找到匹配的記錄,那么我可以返回該數(shù)據(jù)并繼續(xù)下一步,并且可以停止調(diào)用其他數(shù)據(jù)庫
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

獲取值后立即返回該值如何。不允許流程向前移動并打破循環(huán)。


using (var Contexts = instContextfactory.GetContextList())

    {

           foreach(var context in Contexts.GetContextList())

           {    

               // how do I make all the calls and return data from the first call that finds data and continue with the further process.(don't care about other calls if any single call finds data.           

                var result = await context.Insurance.GetInsuranceByANI(ani);


                if(result.Any())

                {

                    return result.First();

                }

           }

    }


查看完整回答
反對 回復(fù) 2023-05-14
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

為了簡單起見,您應(yīng)該首先改回您的GetInsuranceByANI方法以再次同步。稍后我們將生成任務(wù)以異步調(diào)用它。


public IEnumerable<Insurance> GetInsuranceByANI(string ani)

{

    using (ITransaction transaction = Session.Value.BeginTransaction())

    {

        transaction.Rollback();

        IDbCommand command = new SqlCommand();

        command.Connection = Session.Value.Connection;


        transaction.Enlist(command);


        string storedProcName = "spGetInsurance";


        command.CommandText = storedProcName;

        command.Parameters.Add(new SqlParameter("@ANI", SqlDbType.Char, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Default, ani));


        var rdr = command.ExecuteReader();

        return MapInsurance(rdr);

    }

}

現(xiàn)在來實(shí)現(xiàn)異步搜索所有數(shù)據(jù)庫的方法。我們將為每個(gè)數(shù)據(jù)庫創(chuàng)建一個(gè)任務(wù),在線程池線程中運(yùn)行。這是值得商榷的,但我們正在努力讓事情變得簡單。我們還實(shí)例化 a CancellationTokenSource,并將其傳遞Token給所有Task.Run方法。這只會確保在我們得到結(jié)果后,不會再啟動更多任務(wù)。如果線程池中的可用線程多于要搜索的數(shù)據(jù)庫,則所有任務(wù)將立即開始,取消令牌實(shí)際上不會取消任何內(nèi)容。換句話說,無論如何,所有啟動的查詢都將完成。這顯然是一種資源浪費(fèi),但我們再次努力讓事情變得簡單。


啟動任務(wù)后,我們將進(jìn)入一個(gè)等待下一個(gè)任務(wù)完成的循環(huán)(使用方法Task.WhenAny)。如果找到結(jié)果,我們?nèi)∠钆撇⒎祷亟Y(jié)果。如果未找到結(jié)果,我們將繼續(xù)循環(huán)以獲得下一個(gè)結(jié)果。如果所有任務(wù)都完成但我們?nèi)匀粵]有結(jié)果,我們將返回 null。


async Task<IEnumerable<Insurance>> SearchAllByANI(string ani)

{

    var tasks = new HashSet<Task<IEnumerable<Insurance>>>();

    var cts = new CancellationTokenSource();

    using (var Contexts = instContextfactory.GetContextList())

    {

        foreach (var context in Contexts.GetContextList())

        {

            tasks.Add(Task.Run(() =>

            {

                return context.Insurance.GetInsuranceByANI(ani);

            }, cts.Token));

        }

    }

    while (tasks.Count > 0)

    {

        var task = await Task.WhenAny(tasks);

        var result = await task;

        if (result != null && result.Any())

        {

            cts.Cancel();

            return result;

        }

        tasks.Remove(task);

    }

    return null;

}

使用示例:


IEnumerable<Insurance> result = await SearchAllByANI("12345");

if (result == null)

{

    // Nothing fould

}

else

{

    // Do something with result

}


查看完整回答
反對 回復(fù) 2023-05-14
  • 2 回答
  • 0 關(guān)注
  • 166 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號