CefTaskRunner.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Class that asynchronously executes tasks on the associated thread. It is safe
  10. /// to call the methods of this class on any thread.
  11. /// CEF maintains multiple internal threads that are used for handling different
  12. /// types of tasks in different processes. The cef_thread_id_t definitions in
  13. /// cef_types.h list the common CEF threads. Task runners are also available for
  14. /// other CEF threads as appropriate (for example, V8 WebWorker threads).
  15. /// </summary>
  16. public sealed unsafe partial class CefTaskRunner
  17. {
  18. /// <summary>
  19. /// Returns the task runner for the current thread. Only CEF threads will have
  20. /// task runners. An empty reference will be returned if this method is called
  21. /// on an invalid thread.
  22. /// </summary>
  23. public static CefTaskRunner GetForCurrentThread()
  24. {
  25. return CefTaskRunner.FromNativeOrNull(cef_task_runner_t.get_for_current_thread());
  26. }
  27. /// <summary>
  28. /// Returns the task runner for the specified CEF thread.
  29. /// </summary>
  30. public static CefTaskRunner GetForThread(CefThreadId threadId)
  31. {
  32. return CefTaskRunner.FromNativeOrNull(cef_task_runner_t.get_for_thread(threadId));
  33. }
  34. /// <summary>
  35. /// Returns true if this object is pointing to the same task runner as |that|
  36. /// object.
  37. /// </summary>
  38. public bool IsSame(CefTaskRunner that)
  39. {
  40. return cef_task_runner_t.is_same(_self, that.ToNative()) != 0;
  41. }
  42. /// <summary>
  43. /// Returns true if this task runner belongs to the current thread.
  44. /// </summary>
  45. public bool BelongsToCurrentThread
  46. {
  47. get { return cef_task_runner_t.belongs_to_current_thread(_self) != 0; }
  48. }
  49. /// <summary>
  50. /// Returns true if this task runner is for the specified CEF thread.
  51. /// </summary>
  52. public bool BelongsToThread(CefThreadId threadId)
  53. {
  54. return cef_task_runner_t.belongs_to_thread(_self, threadId) != 0;
  55. }
  56. /// <summary>
  57. /// Post a task for execution on the thread associated with this task runner.
  58. /// Execution will occur asynchronously.
  59. /// </summary>
  60. public bool PostTask(CefTask task)
  61. {
  62. return cef_task_runner_t.post_task(_self, task.ToNative()) != 0;
  63. }
  64. /// <summary>
  65. /// Post a task for delayed execution on the thread associated with this task
  66. /// runner. Execution will occur asynchronously. Delayed tasks are not
  67. /// supported on V8 WebWorker threads and will be executed without the
  68. /// specified delay.
  69. /// </summary>
  70. public bool PostDelayedTask(CefTask task, long delay)
  71. {
  72. return cef_task_runner_t.post_delayed_task(_self, task.ToNative(), delay) != 0;
  73. }
  74. }
  75. }