AsyncOp.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// Object you may use to check on the results of an asynchronous operation. Contains uninitialized data until
  9. /// <see cref="IsCompleted"/> returns true.
  10. /// </summary>
  11. public class AsyncOp : ScriptObject
  12. {
  13. /// <summary>
  14. /// Constructs a new async operation.
  15. /// </summary>
  16. internal AsyncOp() // Note: For internal runtime use only
  17. {
  18. Internal_CreateInstance(this);
  19. }
  20. /// <summary>
  21. /// Checks has the asynchronous operation completed.
  22. /// </summary>
  23. public bool IsCompleted
  24. {
  25. get
  26. {
  27. bool value;
  28. Internal_IsComplete(mCachedPtr, out value);
  29. return value;
  30. }
  31. }
  32. /// <summary>
  33. /// Retrieves the value returned by the async operation. Only valid if <see cref="IsCompleted"/> returns true.
  34. /// </summary>
  35. /// <typeparam name="T">Type of the return value. Caller must ensure to provide the valid type.</typeparam>
  36. /// <returns>Return value of the async operation.</returns>
  37. public T GetReturnValue<T>()
  38. {
  39. return (T)Internal_GetReturnValue(mCachedPtr);
  40. }
  41. /// <summary>
  42. /// Blocks the calling thread until asynchronous operation completes.
  43. /// </summary>
  44. public void BlockUntilComplete()
  45. {
  46. Internal_BlockUntilComplete(mCachedPtr);
  47. }
  48. [MethodImpl(MethodImplOptions.InternalCall)]
  49. internal static extern void Internal_CreateInstance(AsyncOp managedInstance);
  50. [MethodImpl(MethodImplOptions.InternalCall)]
  51. internal static extern void Internal_IsComplete(IntPtr thisPtr, out bool value);
  52. [MethodImpl(MethodImplOptions.InternalCall)]
  53. internal static extern object Internal_GetReturnValue(IntPtr thisPtr);
  54. [MethodImpl(MethodImplOptions.InternalCall)]
  55. internal static extern void Internal_BlockUntilComplete(IntPtr thisPtr);
  56. }
  57. }