异步:大班的练习是什么?

时间:2017-10-17 09:05:34

标签: c# asynchronous async-await task

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

理解的例子:

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规则兼容?

感谢您的时间

1 个答案:

答案 0 :(得分:3)

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