SynchronizationContextExtensions.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. using System.Threading;
  5. namespace System.Reactive.Concurrency
  6. {
  7. internal static class SynchronizationContextExtensions
  8. {
  9. public static void PostWithStartComplete<T>(this SynchronizationContext context, Action<T> action, T state)
  10. {
  11. context.OperationStarted();
  12. context.Post(
  13. o =>
  14. {
  15. try
  16. {
  17. action((T)o);
  18. }
  19. finally
  20. {
  21. context.OperationCompleted();
  22. }
  23. },
  24. state
  25. );
  26. }
  27. public static void PostWithStartComplete(this SynchronizationContext context, Action action)
  28. {
  29. context.OperationStarted();
  30. context.Post(
  31. _ =>
  32. {
  33. try
  34. {
  35. action();
  36. }
  37. finally
  38. {
  39. context.OperationCompleted();
  40. }
  41. },
  42. null
  43. );
  44. }
  45. }
  46. }