Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Periodically check `AsyncOperation`s for errors?

Discussion in 'Scripting' started by The_Elemental_of_Destruction, Mar 17, 2019.

  1. The_Elemental_of_Destruction

    The_Elemental_of_Destruction

    Joined:
    Feb 12, 2019
    Posts:
    3
    I'm working on a progress bar that manages an `AsyncOperation` that has been passed to it. However, some of the operations were not created by me and often don't handle exceptions in a way that is easy to check for (like how the `UnityWebRequest` has the `isError` property so you can easily see that it had an error). My progress bar updates every frame using the `Update` method, which currently only consists of a check to `AsyncOperation.isDone`.

    My hope was originally that I could have a seperate object for error handling like this:
    Code (CSharp):
    1. public class AsyncErrorHandler
    2. {
    3.     public bool isError { get; private set; }
    4.     public bool isDone { get; private set; }
    5.     private readonly AsyncOperation a;
    6.     public AsyncErrorHandler(AsyncOperation op)
    7.     {
    8.         isError = false;
    9.         isDone = false;
    10.         a = op;
    11.     }
    12.  
    13.     public async Task StartChecker()
    14.     {
    15.         try { await a; }
    16.         catch
    17.         {
    18.             isError = true; // I don't care WHAT the error is, just that it had one.
    19.         }
    20.     }
    21. }
    22.  
    Unfortunately, I get the error that `AsyncOperation` has no definition for `GetAwaiter`, so I have no idea how to go from here. Can anyone help me with exception detection and handling in `AsyncOperation`?
     
  2. The_Elemental_of_Destruction

    The_Elemental_of_Destruction

    Joined:
    Feb 12, 2019
    Posts:
    3
    I think I should also mention that sometimes something will happen in the async operations that causes some kind of exception that completely crashes the program, And I want to try to catch the exception before it propagates all the way up the line if I can.