SynchronizationContextExtensions.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !NO_SYNCCTX
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. namespace System.Reactive.Concurrency
  11. {
  12. internal static class SynchronizationContextExtensions
  13. {
  14. public static void PostWithStartComplete<T>(this SynchronizationContext context, Action<T> action, T state)
  15. {
  16. context.OperationStarted();
  17. context.Post(
  18. o =>
  19. {
  20. try
  21. {
  22. action((T)o);
  23. }
  24. finally
  25. {
  26. context.OperationCompleted();
  27. }
  28. },
  29. state
  30. );
  31. }
  32. public static void PostWithStartComplete(this SynchronizationContext context, Action action)
  33. {
  34. context.OperationStarted();
  35. context.Post(
  36. _ =>
  37. {
  38. try
  39. {
  40. action();
  41. }
  42. finally
  43. {
  44. context.OperationCompleted();
  45. }
  46. },
  47. null
  48. );
  49. }
  50. }
  51. }
  52. #endif