gpt4 book ai didi

c# - 调试时异步行为同步?

转载 作者:行者123 更新时间:2023-11-30 16:49:33 24 4
gpt4 key购买 nike

我对 async/await 的理解肯定存在缺陷。我想要一段名为 SaveSearchCase 的代码在后台异步运行。

我希望它被触发并忘记它并继续当前方法的 return 语句。

public IList<Entities.Case.CreateCaseOutput> createCase(ARC.Donor.Data.Entities.Case.CreateCaseInput CreateCaseInput, ARC.Donor.Data.Entities.Case.SaveCaseSearchInput SaveCaseSearchInput)
{
..........
..........
..........
var AcctLst = rep.ExecuteStoredProcedure<Entities.Case.CreateCaseOutput>(strSPQuery, listParam).ToList();

if (!string.IsNullOrEmpty(AcctLst.ElementAt(0).o_case_seq.ToString()))
{
Task<IList<Entities.Case.SaveCaseSearchOutput>> task = saveCaseSearch(SaveCaseSearchInput, AcctLst.ElementAt(0).o_case_seq);
Task t = task.ContinueWith(
r => { Console.WriteLine(r.Result); }
);
}
Console.WriteLine("After the async call");
return AcctLst;
}

SaveCaseSearch 看起来像

public async Task<IList<Entities.Case.SaveCaseSearchOutput>> saveCaseSearch(ARC.Donor.Data.Entities.Case.SaveCaseSearchInput SaveCaseSearchInput,Int64? case_key)
{
Repository rep = new Repository();
string strSPQuery = string.Empty;
List<object> listParam = new List<object>();
SQL.CaseSQL.getSaveCaseSearchParameters(SaveCaseSearchInput, case_key,out strSPQuery, out listParam);
var AcctLst = await rep.ExecuteStoredProcedureAsync<Entities.Case.SaveCaseSearchOutput>(strSPQuery, listParam);
return (System.Collections.Generic.IList<ARC.Donor.Data.Entities.Case.SaveCaseSearchOutput>)AcctLst;
}

但是当我看到调试器的 createCase 方法首先等待 SaveCaseSearch 完成,然后才完成

它打印“After Async Call”

然后返回。我绝对不想要。

那么我的理解在哪方面有缺陷?请帮助使其异步运行并继续当前方法的打印返回语句

更新

我更新了 SaveCaseSearch 方法以反射(reflect)如下:

public async Task<IList<Entities.Case.SaveCaseSearchOutput>> saveCaseSearch(ARC.Donor.Data.Entities.Case.SaveCaseSearchInput SaveCaseSearchInput,Int64? case_key)
{
return Task.Run<IList<Entities.Case.SaveCaseSearchOutput>>(async (SaveCaseSearchInput, case_key) =>
{
Repository rep = new Repository();
string strSPQuery = string.Empty;
List<object> listParam = new List<object>();
SQL.CaseSQL.getSaveCaseSearchParameters(SaveCaseSearchInput, case_key, out strSPQuery, out listParam);
var AcctLst = await rep.ExecuteStoredProcedureAsync<Entities.Case.SaveCaseSearchOutput>(strSPQuery, listParam);
return (System.Collections.Generic.IList<ARC.Donor.Data.Entities.Case.SaveCaseSearchOutput>)AcctLst;
});
}

但是参数有问题。它说

Error   4   A local variable named 'SaveCaseSearchInput' cannot be declared in this scope because it would give a different meaning to 'SaveCaseSearchInput', which is already used in a 'parent or current' scope to denote something else C:\Users\m1034699\Desktop\Stuart_V2_12042016\Stuart Web Service\ARC.Donor.Data\Case\Search.cs   43  79  ARC.Donor.Data

最佳答案

saveCaseSearch() 方法在主线程中同步运行,这是这里的主要问题。与其返回带有任务的结果,不如返回带有操作本身的任务。这是一些简化的示例:

Runs synchronously and waits 5 seconds

    public IList<int> A()
{

var AcctLst = new List<int> { 0, 2, 5, 8 };

if (true)
{
Task<IList<int>> task = saveCaseSearch();

Task t = task.ContinueWith(
r => { Console.WriteLine(r.Result[0]); }
);
}

Console.WriteLine("After the async call");

return AcctLst;
}

// runs sync and in the end returns Task that is never actually fired
public async Task<IList<int>> saveCaseSearch()
{
Thread.Sleep(5000);
return new List<int>() { 10, 12, 16 };
}

Runs asynchronously - fires task & forgets :

    public IList<int> A()
{
... same code as above
}

// notice that we removed `async` keyword here because we just return task.
public Task<IList<int>> saveCaseSearch()
{
return Task.Run<IList<int>>(() =>
{
Thread.Sleep(5000);
return new List<int>() { 10, 12, 16 };
});
}

这里是 full code for this example

关于c# - 调试时异步行为同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36592445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com