I have a C# WebAPI that runs a fairly complex algorithm when called from the client. The algorithm is a bit unpredictable in the amount of time it can take - usually 5-10 seconds but occasionally up to 90 seconds (I'm working on this). As the user manipulates the UI on the client side, I'd like to continuously send up requests to the API to begin processing, such that when the user enters a different section of the UI, the API request has completed and I can immediately display the results of the (most recent) API call.
It's only important to finish the calculations the final time they are run, so I'd like new requests to this API method to replace the old requests if any are running, rather than spawn a new thread in the API.
In case that's unclear:
- User manipulates UI.
- UI sends RequestA to WebAPI (WebAPI begins calculations)
- User manipulates UI again
- UI sends RequestB to WebAPI
- WebAPI is not finished with RequestA yet, so RequestA is killed and RequestB begins
Is there a way to do this, or if not (or if it's terrible practice) is there a different way to create this kind of functionality?