0

How can I check if an awaited Async method is running or not?

I want to create a timer that checks an awaited Async method some time after the Async was called. It will check whether it's still running or whether it had something like an OutOfMemoryException which will not be caught by a try-catch in the caller method - the call will never return.

EDIT

This async method might be awaited by another async method, and so on. Is there a way to check if the top async contains any asyncs that will never return?

(I'm trying to test it now, but am having a hard time creating an exception that will not be caught. I tried creating a StackOverflowException but it was caught by Visual Studio. Perhaps because it's my code, not some built-in async.)

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • 1
    An async method returns a Task. You can use the status of the Task to know what's going on – Kevin Gosse Jun 30 '16 at 20:59
  • @KooKiz Thanks. Actually, this one returns an `IAsyncAction`. But I see what you mean. Do you know if the `IAsyncAction` will be faulty or will its `Status` throw an exception? (I see now I can cast it to a `Task`. Anyway, I wonder how I'm supposed to check for a non-returning Task.) – ispiro Jun 30 '16 at 21:01
  • If the task is `await`ed, then any exceptions will be observed. – Stephen Cleary Jun 30 '16 at 21:11
  • @StephenCleary See [my previous question](http://stackoverflow.com/questions/38130987/finally-not-executed-because-of-async-in-try) where I mention that a `finally` block will _not_ always be executed as mentioned [here](http://stackoverflow.com/a/111617/939213). And as per [a comment on my question](http://stackoverflow.com/questions/38130987/finally-not-executed-because-of-async-in-try#comment63694719_38130987) - this might be because the Async never returns. If that's all wrong - perhaps you could help me out there. – ispiro Jun 30 '16 at 21:20
  • @StephenCleary I also added an edit to this question. – ispiro Jun 30 '16 at 21:41
  • 1
    @ispiro: Your previous question does not contain a [verifiable example](http://stackoverflow.com/help/mcve). Reducing to a verifiable example is the first step in diagnosing your problem. – Stephen Cleary Jun 30 '16 at 22:54

0 Answers0