gpt4 book ai didi

c# - 异步 : What practices in large classes?

转载 作者:行者123 更新时间:2023-11-30 15:56:27 25 4
gpt4 key购买 nike

大类完全异步兼容是个好主意还是应该使用其他方法?

理解示例:

AsyncValidateUserInput.cs

public async Task<TaskStateHelper> CheckSiteIsReachable()
public async Task<TaskStateHelper> VerifyCertificate()
public async Task<TaskStateHelper> TestAccount()
{
#DoStuff
await exchangeAccount.FetchAccountInformation
#DoStuff
await exchangeAccount.GetAccountBalance
#DoStuff
}

GetAccountBalance 基于 AccountManager 类:

AccountManager.cs

public async Task<decimal> GetAccountBalance()
{
#DoStuff
await FooAPI.GetAccountBalanceAsync()
}

FooAPI.cs

public async Task<Dictionary<string, decimal>> GetAccountBalanceAsync()
{
var queryResult = await QueryPrivateAsync("Balance");
#DoStuff
}

private async Task<Stream> QueryPrivateAsync(string method, Dictionary<string, string> param = null)
{
#DoStuff
await postStream.WriteAsync(postData);
#DoStuff
await ReadFromStreamAsync(webResponse.GetResponseStream());
#DoStuff
}

另一种方法是简单地创建一个任务。

result = await Task.Factory.StartNew(exchangeAccount.GetAccountBalance);

我使用的异步规则兼容吗?

谢谢你的时间

最佳答案

是的,无论大小如何,制作任何应该是 Async 的东西通常是个好主意。本文可能有所帮助:https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

关于c# - 异步 : What practices in large classes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46786406/

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