C # runs async task in background when parent object returns

I am writing an MVC5 application that has a controller with say function Method1

I have a third-party function that async Task, let him call it DoMethodAsync(it essentially makes a POST call).

I want to call it right before returning from Method1, but I don't really care about the result of the async task, if it fails, it fails. How can I start and forget and execute it in the background so that it does not block the controller from responding to other requests?

How can I guarantee that DoMethodAsynconly after return Method1?

+4
source share
3

, async, , .

? , .

, ?

, " " ASP.NET.

, . , "" - , .

, Task.Run:

var _ignored = Task.Run(() => DoMethodAsync());

, ASP.NET " ". , DoMethodAsync , ASP.NET, HostingEnvironment.QueueBackgroundWorkItem (4.5.2+) BackgroundTaskManager.Run (4.5.0/4.5.1).

, DoMethodAsync; " ". , .

+9

, ( ) , async, , ( "" ' , "" "" ..).
, , , . , , .

0
public ActionResult Index()
{
    DoMethodAsync(); // if you do not await and simple call this task it will run in backround as you want
    return View();
}

async Task DoMethodAsync()
{
    // calculate something
}
0
source

All Articles