API reference
API reference
Select your platform
No SDKs available
No versions available

OVRTask Class

Extends IEquatable< OVRTask< TResult > >, IDisposable
Static methods related to OVRTask<TResult>.
Represents an awaitable task.
The static class OVRTask provides some utilities for working with tasks.
Many asynchronous methods in the Core SDK return an awaitable OVRTask<TResult>. The methods in this class provide functionality to combine multiple tasks (see WhenAll<T1,T2>) and create your own OVRTask<TResult> (see Create<TResult>). For more information on tasks, see Asynchronous Tasks.
The OVRTask<TResult> struct is a task-like object which supports the await pattern. Typically, you do not need to create or use this object directly. Instead, you can either:
Note this requires the main thread to complete the await contract; therefore, blocking on an OVRTask can result in an infinite loop.
You can also:
For more details, including code samples, see Asynchronous Tasks.

Properties

HasResult : bool
[Get]
Whether there is a result available.
This property is true when the OVRTask<TResult> is complete (IsCompleted is true) and GetResult has not already been called.
Note that GetResult is called implicitly when using await or ContinueWith.
Signature
bool HasResult
IsCompleted : bool
[Get]
Indicates whether the task has completed.
Choose only one pattern out of the three proposed way of awaiting for the task completion: Polling,async/await or ContinueWith(Action<TResult>) as all three patterns will end up calling the GetResult which can only be called once.
Signature
bool IsCompleted
IsFaulted : bool
[Get]
Whether the task completed due to an unhandled exception.
If the task is in a faulted state, then you can extract the exception with GetException.
Signature
bool IsFaulted

Methods

ContinueWith ( onCompleted )
Registers a delegate to be invoked on completion of the task.
The delegate will be invoked with the TResult result as parameter.
Do not use in conjunction with any other methods (await or calling GetResult).
Note: If the task throws an exception during execution, there is no way to catch it in when using ContinueWith. Most Meta XR Core SDK calls that return an OVRTask do not throw, but it is possible to return an OVRTask<TResult> from your own async method, which can still throw. For example,
async OVRTask<OVRAnchor> DoSomethingAsync() {
  var anchor = await OVRAnchor.CreateSpatialAnchorAsync(pose); // doesn't throw
  throw new Exception(); // Cannot be caught if using ContinueWith
  return anchor;
}

async void MethodA() {
  try {
    var anchor = await DoSomethingAsync();
  } catch (Exception e) {
    // okay; exception caught!
  }
}

void MethodB() {
  DoSomethingAsync().ContinueWith(anchor => {
    Debug.Log($"Anchor {anchor} created!");
  });
}
In the above example, the exception generated by DoSomethingAsync is caught in MethodA, but there is no way to catch it in MethodB because it uses ContinueWith rather than await. The exception is still thrown, however.
Signature
void ContinueWith(Action< TResult > onCompleted)
Parameters
onCompleted: Action< TResult >  A delegate to be invoked when this task completes. If the task is already complete, onCompleted is invoked immediately.
Returns
void
Throws
ArgumentNullException  Thrown if onCompleted is null. InvalidOperationException  Thrown if this task is already being awaited. InvalidOperationException  Thrown if this task is already used in another ContinueWith. InvalidOperationException  Thrown if this task is already used as a ValueTask (ToValueTask). InvalidOperationException  Thrown if this task is already used or as an Awaitable. (ToAwaitable, only available in Unity 2023.1+).
ContinueWith< T > ( onCompleted , state )
Registers a delegate that will get called on completion of the task.
The delegate will be invoked with state and the TResult result as parameters. Do not use in conjunction with any other methods (await or calling GetResult).
Note: If the task throws an exception during execution, there is no way to catch it in when using a callback. See ContinueWith(Action<TResult>) for more details.
Signature
void ContinueWith< T >(Action< TResult, T > onCompleted, T state)
Parameters
onCompleted: Action< TResult, T >  A delegate to be invoked when this task completes. If the task is already complete, onCompleted is invoked immediately.
state: T  An object to store and pass to onCompleted .
Returns
void
Throws
ArgumentNullException  Thrown if onCompleted is null. InvalidOperationException  Thrown if this task is already being awaited. InvalidOperationException  Thrown if this task is already used in another ContinueWith. InvalidOperationException  Thrown if this task is already used as a ValueTask (ToValueTask). InvalidOperationException  Thrown if this task is already used or as an Awaitable. (ToAwaitable, only available in Unity 2023.1+).
Dispose ()
Disposes of the task.
Invalidate this object but does not cancel the task. In the case where the result will not actually be consumed, it must be called to prevent a memory leak. You can not call GetResult nor use await on a disposed task.
Signature
void Dispose()
Returns
void
Equals ( other )
Compares for equality with another task.
Signature
bool Equals(OVRTask< TResult > other)
Parameters
other: OVRTask< TResult >  The OVRTask<TResult> to compare for equality with this task.
Returns
bool  Returns true if the two task objects represent the same task, otherwise false.
Equals ( obj )
Compares for equality with an object.
Signature
override bool Equals(object obj)
Parameters
obj: object  The object to compare for equality with this task.
Returns
override bool  Returns true if obj is an OVRTask<TResult> and represents the same task as this one, otherwise false.
GetAwaiter ()
Gets an awaiter that satisfies the await contract.
This allows an OVRTask<TResult> to be awaited using the await keyword. Typically, you should not call this directly; instead, it is invoked by the compiler, e.g.,
// Something that returns an OVRTask<T>
var task = GetResultAsync();

// compiler uses GetAwaiter here
var result = await task;
Or, more commonly:
var result = await GetResultAsync();
Requires the main thread to complete the await contract - blocking can result in an infinite loop.
Signature
Awaiter GetAwaiter()
Returns
Awaiter  Returns an Awaiter-like object that satisfies the await pattern.
GetException ()
Get the exception if the task is in a faulted state.
If IsFaulted is true, then this method gets the exception associated with this method. Similar to GetResult, you can only get the exception once and throws if there is no exception.
When using await or ContinueWith, you do not need to explicitly get the exception. Use this method when you have an exception when it is implicitly created by the compiler and you query the task object directly, as in the following example:
async OVRTask<bool> DoSomethingAsync() {
  var anchor = await OVRAnchor.CreateSpatialAnchorAsync(pose); // <– implicitly generated OVRTask<bool>
  SomeMethodThatThrows();
  return true;
}

OVRTask<bool> task = DoSomethingAsync();

// later...

if (task.IsFaulted) {
  throw task.GetException();
}
Signature
Exception GetException()
Returns
Exception  Returns the Exception associated with the task.
Throws
InvalidOperationException  Thrown if IsFaulted is false.
GetHashCode ()
Generates a hash code suitable for use in a Dictionary or HashSet
Signature
override int GetHashCode()
Returns
override int  Returns a hash code suitable for use in a Dictionary or HashSet
GetResult ()
Gets the result of the asynchronous operation.
This method should only be called once IsCompleted is true. Calling it multiple times will throw InvalidOperationException.
Note that GetResult is called implicitly when using await or ContinueWith. You should not call this method explicitly when using one of those mechanisms.
Signature
TResult GetResult()
Returns
TResult  Returns the result of type TResult .
Throws
InvalidOperationException  Thrown when the task doesn't have any available result. This could happen if the method is called before IsCompleted is true, after the task has been disposed of, if this method has already been called once, or if an exception was thrown during the task's execution.
ToString ()
Generates a string representation of this task, based on its internal id.
Internally, tasks are identified by a System.Guid. This method should only be used for debugging purposes. The implementation or behavior may change in future versions, so you should not rely on the format of this string for other uses.
Signature
override string ToString()
Returns
override string  Returns the stringification of this task's unique identifier (Guid).
ToValueTask ()
Converts the task to a ValueTask.
This method converts this OVRTask<TResult> to a ValueTask.
A ValueTask is similar to an OVRTask. Key differences:
  • A ValueTask does not support ContinueWith
  • A ValueTask does not support WhenAll(System.Collections.Generic.IEnumerable<OVRTask<TResult>>)
The above are only supported on the Task object.
Invoking this method also invalidates this OVRTask<TResult>. It is invalid to continue using an OVRTask<TResult> after calling ToValueTask.
Signature
ValueTask< TResult > ToValueTask()
Returns
ValueTask< TResult >  Returns a new ValueTask that completes when the asynchronous operation completes.
Throws
InvalidOperationException  Thrown if the task is not pending (IsCompleted is true) and does not have a result (HasResult is false) InvalidOperationException  Thrown if the task has already been awaited. InvalidOperationException  Thrown if ContinueWith has already been called.
TryGetResult ( result )
Tries to get the result of the asynchronous operation.
This method may safely be called at any time. It tests whether the operation is both complete (IsCompleted is true) and the result has not already been retrieved with GetResult (HasResult is true).
If the result is available, result is set to the result and this method returns true. This method is equivalent to (though more efficient than) the following:
if (task.HasResult) {
  result = task.GetResult();
  return true;
} else {
  result = default;
  return false;
}
Signature
bool TryGetResult(out TResult result)
Parameters
result: out TResult  Set to the result of the task, if one is available. Otherwise, it is set to the default value for TResult .
Returns
bool true if this task is complete and a result is available.
Throws
InvalidOperationException  Thrown when the task doesn't have any available result. This could happen if the method is called before IsCompleted is true, after the task has been disposed of or if this method has already been called once.

Static Methods

Create< TResult > ( taskId )
Creates a new task.
This method creates a new pending task. When the task completes, set its result with SetResult<TResult>.
The returned task is in a pending state; that is, OVRTask<TResult>.IsCompleted is false until you later set its result with SetResult<TResult>.
The taskId must be unique to the new task. You may use any Guid as long as it has not previously been used to create a task. Use Guid.NewGuid() to generate a random task id.
Signature
static OVRTask< TResult > Create< TResult >(Guid taskId)
Parameters
taskId: Guid  The id used to assign the new task.
Returns
OVRTask< TResult >  Returns a new task which completes when you call SetResult<TResult>.
Throws
ArgumentException  Thrown if taskId refers to an existing task.
FromResult< TResult > ( result )
Creates an already-complete task.
This creates a completed task whose result is result .
Signature
static OVRTask< TResult > FromResult< TResult >(TResult result)
Parameters
result: TResult  The result of the task.
Returns
OVRTask< TResult >  Returns a new, completed task-like object whose result is result .
operator!= ( lhs , rhs )
Compares two tasks for inequality.
This is the logical negation of Equals(OVRTask<TResult>).
Signature
static bool operator!=(OVRTask< TResult > lhs, OVRTask< TResult > rhs)
Parameters
lhs: OVRTask< TResult >  The task to compare with rhs .
rhs: OVRTask< TResult >  The task to compare with lhs .
Returns
bool  Returns true if lhs is not equal to rhs , otherwise false.
operator== ( lhs , rhs )
Compares two tasks for equality.
This is the same equality test as Equals(OVRTask<TResult>).
Signature
static bool operator==(OVRTask< TResult > lhs, OVRTask< TResult > rhs)
Parameters
lhs: OVRTask< TResult >  The task to compare with rhs .
rhs: OVRTask< TResult >  The task to compare with lhs .
Returns
bool  Returns true if lhs is equal to rhs , otherwise false.
SetResult< TResult > ( id , result )
Sets the result of a pending task.
Set the result of a task previously created with Create<TResult>. When this method returns, OVRTask<TResult>.IsCompleted will be true.
OVRTask<int> MyOpAsync() {
  _id = Guid.NewGuid();
  var task = OVRTask.Create<int>(id);
  return task;
}

// later, when the task completes:
void Update() {
  if (operationComplete) {
    OVRTask.SetResult(_id, result);
  }
}
This allows you to await on MyOpAsync:
async void OnButtonPressed() {
  var result = await MyOpAsync();
}
Signature
static void SetResult< TResult >(Guid id, TResult result)
Parameters
id: Guid  The task's unique id.
result: TResult  The result the task should have.
Returns
void
Throws
InvalidOperationException  Thrown if the task with id id already has a result.
WhenAll< T1, T2 > ( task1 , task2 )
Creates a new task that will complete when all of the given tasks have completed.
This method can be used to combine multiple tasks into a single task that depends on all of them. When the returned task completes, the results of all combined tasks can be accessed through the result of the returned task. The type of the result is a tuple containing the result types of each input task, that is
ValueTuple<T1, T2>
For example,
var (result1, result2) = await OVRTask.Combine(task1, task2);
Signature
static OVRTask<(T1, T2)> WhenAll< T1, T2 >(OVRTask< T1 > task1, OVRTask< T2 > task2)
Parameters
task1: OVRTask< T1 >  The first task to combine
task2: OVRTask< T2 >  The second task to combine
Returns
OVRTask<(T1, T2)>  Returns a task that represents the completion of all input tasks.
WhenAll< T1, T2, T3 > ( task1 , task2 , task3 )
Creates a new task that will complete when all of the given tasks have completed.
This method can be used to combine multiple tasks into a single task that depends on all of them. When the returned task completes, the results of all combined tasks can be accessed through the result of the returned task. The type of the result is a tuple containing the result types of each input task, that is
ValueTuple<T1, T2, T3>
For example,
var (result1, result2, result3) = await OVRTask.Combine(task1, task2, task3);
Signature
static OVRTask<(T1, T2, T3)> WhenAll< T1, T2, T3 >(OVRTask< T1 > task1, OVRTask< T2 > task2, OVRTask< T3 > task3)
Parameters
task1: OVRTask< T1 >  The first task to combine
task2: OVRTask< T2 >  The second task to combine
task3: OVRTask< T3 >  The third task to combine
Returns
OVRTask<(T1, T2, T3)>  Returns a task that represents the completion of all input tasks.
WhenAll< T1, T2, T3, T4 > ( task1 , task2 , task3 , task4 )
Creates a new task that will complete when all of the given tasks have completed.
This method can be used to combine multiple tasks into a single task that depends on all of them. When the returned task completes, the results of all combined tasks can be accessed through the result of the returned task. The type of the result is a tuple containing the result types of each input task, that is
ValueTuple<T1, T2, T3, T4>
For example,
var (result1, result2, result3, result4) = await OVRTask.Combine(task1, task2, task3, task4);
Signature
static OVRTask<(T1, T2, T3, T4)> WhenAll< T1, T2, T3, T4 >(OVRTask< T1 > task1, OVRTask< T2 > task2, OVRTask< T3 > task3, OVRTask< T4 > task4)
Parameters
task1: OVRTask< T1 >  The first task to combine
task2: OVRTask< T2 >  The second task to combine
task3: OVRTask< T3 >  The third task to combine
task4: OVRTask< T4 >  The forth task to combine
Returns
OVRTask<(T1, T2, T3, T4)>  Returns a task that represents the completion of all input tasks.
WhenAll< T1, T2, T3, T4, T5 > ( task1 , task2 , task3 , task4 , task5 )
Creates a new task that will complete when all of the given tasks have completed.
This method can be used to combine multiple tasks into a single task that depends on all of them. When the returned task completes, the results of all combined tasks can be accessed through the result of the returned task. The type of the result is a tuple containing the result types of each input task, that is
ValueTuple<T1, T2, T3, T4, T5>
For example,
var (result1, result2, result3, result4, result5) = await OVRTask.Combine(task1, task2, task3, task4, task5);
Signature
static OVRTask<(T1, T2, T3, T4, T5)> WhenAll< T1, T2, T3, T4, T5 >(OVRTask< T1 > task1, OVRTask< T2 > task2, OVRTask< T3 > task3, OVRTask< T4 > task4, OVRTask< T5 > task5)
Parameters
task1: OVRTask< T1 >  The first task to combine
task2: OVRTask< T2 >  The second task to combine
task3: OVRTask< T3 >  The third task to combine
task4: OVRTask< T4 >  The forth task to combine
task5: OVRTask< T5 >  The fifth task to combine
Returns
OVRTask<(T1, T2, T3, T4, T5)>  Returns a task that represents the completion of all input tasks.
WhenAll< T1, T2, T3, T4, T5, T6 > ( task1 , task2 , task3 , task4 , task5 , task6 )
Creates a new task that will complete when all of the given tasks have completed.
This method can be used to combine multiple tasks into a single task that depends on all of them. When the returned task completes, the results of all combined tasks can be accessed through the result of the returned task. The type of the result is a tuple containing the result types of each input task, that is
ValueTuple<T1, T2, T3, T4, T5, T6>
For example,
var (result1, result2, result3, result4, result5, result6) = await OVRTask.Combine(task1, task2, task3, task4, task5, task6);
Signature
static OVRTask<(T1, T2, T3, T4, T5, T6)> WhenAll< T1, T2, T3, T4, T5, T6 >(OVRTask< T1 > task1, OVRTask< T2 > task2, OVRTask< T3 > task3, OVRTask< T4 > task4, OVRTask< T5 > task5, OVRTask< T6 > task6)
Parameters
task1: OVRTask< T1 >  The first task to combine
task2: OVRTask< T2 >  The second task to combine
task3: OVRTask< T3 >  The third task to combine
task4: OVRTask< T4 >  The forth task to combine
task5: OVRTask< T5 >  The fifth task to combine
task6: OVRTask< T6 >  The sixth task to combine
Returns
OVRTask<(T1, T2, T3, T4, T5, T6)>  Returns a task that represents the completion of all input tasks.
WhenAll< T1, T2, T3, T4, T5, T6, T7 > ( task1 , task2 , task3 , task4 , task5 , task6 , task7 )
Creates a new task that will complete when all of the given tasks have completed.
This method can be used to combine multiple tasks into a single task that depends on all of them. When the returned task completes, the results of all combined tasks can be accessed through the result of the returned task. The type of the result is a tuple containing the result types of each input task, that is
ValueTuple<T1, T2, T3, T4, T5, T6, T7>
For example,
var (result1, result2, result3, result4, result5, result6, result7) = await OVRTask.Combine(task1, task2, task3, task4, task5, task6, task7);
Signature
static OVRTask<(T1, T2, T3, T4, T5, T6, T7)> WhenAll< T1, T2, T3, T4, T5, T6, T7 >(OVRTask< T1 > task1, OVRTask< T2 > task2, OVRTask< T3 > task3, OVRTask< T4 > task4, OVRTask< T5 > task5, OVRTask< T6 > task6, OVRTask< T7 > task7)
Parameters
task1: OVRTask< T1 >  The first task to combine
task2: OVRTask< T2 >  The second task to combine
task3: OVRTask< T3 >  The third task to combine
task4: OVRTask< T4 >  The forth task to combine
task5: OVRTask< T5 >  The fifth task to combine
task6: OVRTask< T6 >  The sixth task to combine
task7: OVRTask< T7 >  The seventh task to combine
Returns
OVRTask<(T1, T2, T3, T4, T5, T6, T7)>  Returns a task that represents the completion of all input tasks.
WhenAll< T1, T2, T3, T4, T5, T6, T7, T8 > ( task1 , task2 , task3 , task4 , task5 , task6 , task7 , task8 )
Creates a new task that will complete when all of the given tasks have completed.
This method can be used to combine multiple tasks into a single task that depends on all of them. When the returned task completes, the results of all combined tasks can be accessed through the result of the returned task. The type of the result is a tuple containing the result types of each input task, that is
ValueTuple<T1, T2, T3, T4, T5, T6, T7, T8>
For example,
var (result1, result2, result3, result4, result5, result6, result7, result8) = await OVRTask.Combine(task1, task2, task3, task4, task5, task6, task7, task8);
Signature
static OVRTask<(T1, T2, T3, T4, T5, T6, T7, T8)> WhenAll< T1, T2, T3, T4, T5, T6, T7, T8 >(OVRTask< T1 > task1, OVRTask< T2 > task2, OVRTask< T3 > task3, OVRTask< T4 > task4, OVRTask< T5 > task5, OVRTask< T6 > task6, OVRTask< T7 > task7, OVRTask< T8 > task8)
Parameters
task1: OVRTask< T1 >  The first task to combine
task2: OVRTask< T2 >  The second task to combine
task3: OVRTask< T3 >  The third task to combine
task4: OVRTask< T4 >  The forth task to combine
task5: OVRTask< T5 >  The fifth task to combine
task6: OVRTask< T6 >  The sixth task to combine
task7: OVRTask< T7 >  The seventh task to combine
task8: OVRTask< T8 >  The eighth task to combine
Returns
OVRTask<(T1, T2, T3, T4, T5, T6, T7, T8)>  Returns a task that represents the completion of all input tasks.
WhenAll< TResult > ( tasks )
Creates a task that completes when all of the supplied tasks have completed.
This can be used to combine multiple tasks into a single task. The returned task completes when all tasks in tasks complete.
The result of the returned task is an array containing the results of each individual task. The results are arranged in the same order as the original tasks list.
Signature
static OVRTask< TResult[]> WhenAll< TResult >(IEnumerable< OVRTask< TResult > > tasks)
Parameters
tasks: IEnumerable< OVRTask< TResult > >  The tasks to combine
Returns
OVRTask< TResult[]>  Returns a new task which is completed when all tasks have completed.
Throws
ArgumentNullException  Thrown if tasks is null.
WhenAll< TResult > ( tasks , results )
Creates a task that completes when all of the supplied tasks have completed.
This can be used to combine multiple tasks into a single task. The returned task completes when all tasks in tasks complete.
The result of each task in tasks is added to results . The results are in the same order as tasks .
The list in the combined task is a reference to results . This allows the caller to own (and potentially reuse) the memory for the list of results. It is undefined behavior to access results before the returned task completes.
Signature
static OVRTask< List< TResult > > WhenAll< TResult >(IEnumerable< OVRTask< TResult > > tasks, List< TResult > results)
Parameters
tasks: IEnumerable< OVRTask< TResult > >  The tasks to combine
results: List< TResult >  A list to store the results in. The list is cleared before adding any results to it.
Returns
OVRTask< List< TResult > >  Returns a new task which completes when all tasks are complete.
Throws
ArgumentNullException  Thrown if tasks is null. ArgumentNullException  Thrown if results is null.

Inner Struct

Awaiter Struct

Extends INotifyCompletion
Definition of an awaiter that satisfies the await contract.
This allows an OVRTask<TResult> to be awaited using the await keyword. Typically, you should not use this struct; instead, it is used by the compiler by automatically calling the GetAwaiter method when using the await keyword.

Properties

IsCompleted : bool
[Get]
Whether the task has completed.
When true the asynchronous operation associated with the OVRTask<TResult> that created this Awaiter (see OVRTask<TResult>.GetAwaiter) is complete.
Typically, you would not call this directly. This is queried by a compiler-generated state machine to support async / await.
Signature
bool IsCompleted

Methods

GetResult ()
Gets the result of the asynchronous operation.
Typically, you should not call this directly. Use OVRTask<TResult>.GetResult() instead.
Signature
TResult GetResult()
Returns
TResult  The result of the asynchronous operation.
Throws
InvalidOperationException  Thrown if there is no result available.
OnCompleted ( continuation )
Provides the Awaiter with a method to call when the task completes.
Do not call this directly. It is called by a compiler-generated state machine when using the await keyword.
Signature
void INotifyCompletion. OnCompleted(Action continuation)
Parameters
continuation: Action  The continuation to invoke when the task is complete.
Returns
void INotifyCompletion.