0

NewbieQ: wondering about C# async / await

I have a server-side ajax request handler that is now taking too long to complete causing timeout errors.

The handler calls a method:

public bool Run(Guid jobId) // returns true if successful in running jobId

the job runs a sequence of operations that are alternately IO bound then CPU bound, in case that matters.

Ideally, I'd like to be able to launch the Run, then from time to time query the status of the run, and then get the result (the bool that Run returns).

What's the best practice when writing server-side code to do this?

Can I write a wrapper, like this?

public async Task<bool> RunAsync(Guid jobId) {...}

if so, how does that call the non-async Run?

Or do I need to do a complete re-write of the Run method? Ultimately, I'm calling this from an ajax request handler, so I need to either return the job result, or, I could change the requests so I launch the job then query from time to time for a result that has {bool resultReady, bool? result} or some struct like that.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Steve L
  • 1,523
  • 3
  • 17
  • 24
  • async/await is helpful if you have multiple processes that can run in parallel. If this is a single process that is running and timing out, async/await won't help you. It sounds more like you should have another process, such as a console app, that receives a notification to start doing this work. A console app would not time out like a web request would, but you'd have to poll for the result. Alternatively, you could look into increasing the amount of time before IIS/ASP.NET time out.https://stackoverflow.com/questions/2414441/how-to-increase-request-timeout-in-iis. – ps2goat Sep 25 '17 at 16:19
  • Understood, but this is running on a web service where there are other users with other things to do, and indeed even the user who asked for this task may wish to do other things while they wait for the results. The real concern for me is that the web service itself is timing out waiting for the task to complete as a synchronous task. – Steve L Sep 25 '17 at 17:02
  • @ps2goat That's just not true. `await` is useful for managing asynchronous operations. It doesn't matter whether those operations are doing work in parallel or not. – Servy Sep 25 '17 at 19:24
  • @Servy- I probably phrased that incorrectly. What I meant was if there are a set of synchronous steps in the process that is timing out, async await would not be advantageous. E.g., the single process has step 1 -> step 2 -> step n. If each must finish before the next can start, would there be an advantage? – ps2goat Sep 25 '17 at 19:54
  • If steps are IO-bound, then the threads will be free for the time the IO operation is run outside the .Net – VMAtm Sep 29 '17 at 22:37

2 Answers2

1

What you really want isn't something that can be done in a simple, singular method. You really want a queueing system where you can check the status of a given message in the queue. You would essentially have a method that drops something into a queue (such as MSMQ) and another method that returns the status of a given jobId.

Bigsby
  • 952
  • 6
  • 20
1

You could look into hangfire, or signalR the long running process could happen with an event handler to notify the client.

zimmer62
  • 403
  • 3
  • 13