如何在 C# 中只异步调用少数方法?
How to call only few method asynchronously in C#?
我知道这个问题已经被问过很多次关于堆栈溢出的问题,但我正在寻找关于我的下面代码的一些建议。
在我的应用程序中有许多难以修改的同步方法。我无法将所有内容更改为异步等待。但我想 运行 几个异步方法。
我已经为此编写了一些代码。我还添加了有助于理解我的要求的评论。
这是我的代码:
//This class will perform some heavy operation and also going to call an API for tax configuration.
//The original class takes almost 2 sec to respond. Obviously we are refactoring it but also want this class methods to run async way
public static class TaxCalculatorHelper
{
public static Task<double> CalculateTaxAsync(double salary)
{
// I will do some heavy tax calculation here, so I want it to run asynchronously
return Task.FromResult(500.00); // currently returning temporary value
}
}
//The exisiting classes
public class Employee
{
//This method is not going to be async but What I want that Tax calculation which is heavy task that should run asynchronously
public double GetEmployeeFinalSalary(double salary)
{
var taxValue = Task.Run(async () => await TaxCalculatorHelper.CalculateTaxAsync(salary));
//I was doing this
// return taxValue.Result; // I cannot use this because it blocks the calling thread until the asynchronous operation is complete
//Is the below approach correct ?
return taxValue.GetAwaiter().GetResult();
}
}
public class SomeOtherClass
{
private readonly Employee _employee;
public SomeOtherClass()
{
_employee = new Employee();
}
//This will not be async
public void GetEmployeeCtc(double salary)
{
var finalCtc = _employee.GetEmployeeFinalSalary(salary);
}
}
任何人都可以评论并建议我最好的方法吗?
谢谢!!
假设这是一个 UI 应用程序,然后使用 Task.Run
将同步工作推离 UI 线程是一种可接受的方法。在 ASP.NET 个应用程序中不是一个好主意。
根据您的具体情况,您需要决定如何处理 GetEmployeeFinalSalary
。
//This method is not going to be async but What I want that Tax calculation which is heavy task that should run asynchronously
...
// return taxValue.Result; // I cannot use this because it blocks the calling thread until the asynchronous operation is complete
您需要决定 GetEmployeeFinalSalary
是同步还是异步。如果它是同步的,那么它会阻塞调用线程——这就是同步意味着。
我怀疑您 不是 想要阻止调用线程(我假设那是 UI 线程)。在这种情况下,GetEmployeeFinalSalary
必须 是异步的才能释放调用线程:
public async Task<double> GetEmployeeFinalSalaryAsync(double salary)
{
return await Task.Run(async () => await TaxCalculatorHelper.CalculateTaxAsync(salary));
}
我知道这个问题已经被问过很多次关于堆栈溢出的问题,但我正在寻找关于我的下面代码的一些建议。
在我的应用程序中有许多难以修改的同步方法。我无法将所有内容更改为异步等待。但我想 运行 几个异步方法。
我已经为此编写了一些代码。我还添加了有助于理解我的要求的评论。
这是我的代码:
//This class will perform some heavy operation and also going to call an API for tax configuration.
//The original class takes almost 2 sec to respond. Obviously we are refactoring it but also want this class methods to run async way
public static class TaxCalculatorHelper
{
public static Task<double> CalculateTaxAsync(double salary)
{
// I will do some heavy tax calculation here, so I want it to run asynchronously
return Task.FromResult(500.00); // currently returning temporary value
}
}
//The exisiting classes
public class Employee
{
//This method is not going to be async but What I want that Tax calculation which is heavy task that should run asynchronously
public double GetEmployeeFinalSalary(double salary)
{
var taxValue = Task.Run(async () => await TaxCalculatorHelper.CalculateTaxAsync(salary));
//I was doing this
// return taxValue.Result; // I cannot use this because it blocks the calling thread until the asynchronous operation is complete
//Is the below approach correct ?
return taxValue.GetAwaiter().GetResult();
}
}
public class SomeOtherClass
{
private readonly Employee _employee;
public SomeOtherClass()
{
_employee = new Employee();
}
//This will not be async
public void GetEmployeeCtc(double salary)
{
var finalCtc = _employee.GetEmployeeFinalSalary(salary);
}
}
任何人都可以评论并建议我最好的方法吗?
谢谢!!
假设这是一个 UI 应用程序,然后使用 Task.Run
将同步工作推离 UI 线程是一种可接受的方法。在 ASP.NET 个应用程序中不是一个好主意。
根据您的具体情况,您需要决定如何处理 GetEmployeeFinalSalary
。
//This method is not going to be async but What I want that Tax calculation which is heavy task that should run asynchronously
...
// return taxValue.Result; // I cannot use this because it blocks the calling thread until the asynchronous operation is complete
您需要决定 GetEmployeeFinalSalary
是同步还是异步。如果它是同步的,那么它会阻塞调用线程——这就是同步意味着。
我怀疑您 不是 想要阻止调用线程(我假设那是 UI 线程)。在这种情况下,GetEmployeeFinalSalary
必须 是异步的才能释放调用线程:
public async Task<double> GetEmployeeFinalSalaryAsync(double salary)
{
return await Task.Run(async () => await TaxCalculatorHelper.CalculateTaxAsync(salary));
}