gpt4 book ai didi

c# - WebAPI线程

转载 作者:太空狗 更新时间:2023-10-30 00:40:16 24 4
gpt4 key购买 nike

所以我有一个函数在计算过程中有很长的等待时间。我有一个端点需要调用此函数,但它不关心函数是否完成。

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()
{

}
}

最佳答案

使用任务并行库 (TPL) 衍生出一个新线程。

Task.Run(() => new Repository().computeLongFunction());
return Request.CreateReponse(HttpStatusCode.Ok);

关于c# - WebAPI线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29614940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com