简体   繁体   English

从同步运行异步功能以及异步功能C#

[英]Run async function from sync as well as Async function c#

I am sending 'Async' emails. 我正在发送“异步”电子邮件。

I use a common 'Async' function to call the Email function as I don't need to wait for the response for the emails. 我使用通用的“异步”功能来调用电子邮件功能,因为我不需要等待电子邮件的响应。

public Task SendAsync(....)
{
      ....
      return mailClient.SendMailAsync(email);
}

I need to call it from both async and sync functions. 我需要从asyncsync功能调用它。

Calling from async function 从异步函数调用

public async Task<ActionResult> AsyncFunction(...)
{
   ....
   EmailClass.SendAsync(...);
   ....
   // gives runtime error.
   // "An asynchronous module or handler completed while an asynchronous operation was still pending..."
   // Solved by using 'await EmailClass.SendAsync(...);'
}

Calling from sync function 从同步功能呼叫

public ActionResult syncFunction(...)
{
   ....
   EmailClass.SendAsync(...);
   ....
   // gives runtime error. 
   // "An asynchronous operation cannot be started at this time..."
   // Solved by converting the function as above function
}

Both the functions give runtime error which is then solved by using await keyword in async function. 这两个函数都给出了运行时错误,然后可以通过在异步函数中使用await关键字解决该错误。

But by using await it defeats my purpose of running it on background without waiting for response . 但是通过使用await不能达到我running it on background without waiting for response

How do i call the async function without waiting for the response? 如何在不等待响应的情况下调用异步函数?

You can either: 您可以:

A) Create a 'Fire and Forget' request, as you are trying to do. A)按照您的尝试创建一个“ Fire and Forget”请求。

or 要么

B) Await the result of your request async. B)等待您的请求异步结果。

Method A will use 2 threads, the first being your request thread and the second would be the fire and forget thread. 方法A将使用2个线程,第一个是您的请求线程,第二个是即发即忘线程。 This would steal a thread from the request threadpool, causing thread starvation under heavy load. 这会从请求线程池中窃取线程,从而在高负载下导致线程饥饿。

Method B will spend 1 thread when there are things to process.. the request thread that is. 当有事情要处理时,方法B将花费1个线程。

Method B will consume less resources, and while Method A potentially could be a few ms faster, but at the price of a thread(read: expensive!). 方法B将消耗更少的资源,而方法A可能会更快几毫秒,但要以线程为代价(请参阅:昂贵!)。

When using Async/await, the thread is only active when doing CPU work, and is freed up to serve other requests/tasks when doing IO-bound work. 使用异步/等待时,线程仅在执行CPU工作时才处于活动状态,并在进行IO绑定工作时被释放以服务于其他请求/任务。

While initiating a new thread, will block that thread until it is done (unless you wanna do some complicated thread synchronization). 在启动新线程时,将阻塞该线程直到完成(除非您想执行一些复杂的线程同步)。

TL;DR : Async/Await is way more efficient, and you will end up starving your webserver, if you choose to use Fire and Forget. TL; DR :Async / Await效率更高,如果您选择使用Fire and Forget,最终将使您的Web服务器饿死。

If you still want to run background tasks, read this blog post: Search Results How to run Background Tasks in ASP.NET - Scott Hanselman 如果仍然要运行后台任务,请阅读此博客文章:搜索结果如何在ASP.NET中运行后台任务-Scott Hanselman

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM