0

I'm looking to modify an api method we currently use in our app. For some reason, when it gets called it fires off a long running thread. This allows for a quick response back to the client. I think that the manner in which this is done is incorrect to begin with. The second problem is that when the thread is running for ~ 2 hours it just stops.

Is there a way to dictate how long a thread stays alive for, or would it be better to use a task?

    [HttpPost]
    public void GenerateAndStore1095Cs(int tokenValue, int organizationId, int taxYear)
    {
        AuthenticateAndRecord(tokenValue, nameof(GenerateAndStore1095Cs), nameof(GenerateAndStore1095Cs), organizationId);
        new Thread(() =>
        {
            PDFBusiness.GenerateAndStore1095Cs(organizationId, taxYear);                
        }).Start();
    }
CBC_NS
  • 1,961
  • 4
  • 27
  • 47
  • 1
    APIs aren't designed for such long-running operations. – Big Daddy Apr 03 '18 at 15:44
  • 5
    You should not be launching threads in your web application. Have an application specifically dedicated to long running jobs process this long running task. Generally this means a console app or cloud equivalent, processing jobs off a message queue or running something like [Hangfire](https://www.hangfire.io/). – mason Apr 03 '18 at 15:44
  • if you use .net core look into this: https://learn.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/background-tasks-with-ihostedservice – ekim boran Apr 03 '18 at 15:47
  • Mason - we do somewhat the same by running windows services on a scheduler. This would require an app server outside of an IIS server. – JazzmanJim Apr 03 '18 at 17:18

0 Answers0