繁体   English   中英

WebAPI线程

[英]WebAPI Threading

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

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);

它看起来不像computeLongFunction()返回任何东西,所以试试这个:

Thread longThread = new Thread(() => new Repository().computeLongFunction());
longThread.Start();

return Request.CreateResponse(HttpStatusCode.Ok);

声明线程,以便您仍然能够控制其生命周期。

暂无
暂无

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

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