EK_*_*Day 4 c# multithreading asp.net-web-api
所以我有一个在计算过程中有很长等待时间的函数.我有一个需要调用此函数的端点,但它并不关心函数的完成.
public HttpResponseMessage endPoint
{
Repository repo= new Repository();
// I want repo.computeLongFunction(); to be called, however this endpoint
// can return a http status code "ok" even if the long function hasn't completed.
repo.computeLongFunction();
return Request.CreateReponse(HttpStatusCode.Ok);
}
// If I make the function async would that work?
public class Repository
{
public void compluteLongFunction()
{
}
}
Run Code Online (Sandbox Code Playgroud)
使用任务并行库(TPL)来分离新线程.
Task.Run(() => new Repository().computeLongFunction());
return Request.CreateReponse(HttpStatusCode.Ok);
Run Code Online (Sandbox Code Playgroud)