AsyncOp.cs 2.1 KB

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