AsyncOp.cs 2.5 KB

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