UserWorkItem.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Disposables;
  5. namespace System.Reactive.Concurrency
  6. {
  7. internal sealed class UserWorkItem<TState> : IDisposable
  8. {
  9. private IDisposable? _cancelRunDisposable;
  10. private IDisposable? _cancelQueueDisposable;
  11. private readonly TState _state;
  12. private readonly IScheduler _scheduler;
  13. private readonly Func<IScheduler, TState, IDisposable> _action;
  14. public UserWorkItem(IScheduler scheduler, TState state, Func<IScheduler, TState, IDisposable> action)
  15. {
  16. _state = state;
  17. _action = action;
  18. _scheduler = scheduler;
  19. }
  20. public void Run()
  21. {
  22. if (!Disposable.GetIsDisposed(ref _cancelRunDisposable))
  23. {
  24. Disposable.SetSingle(ref _cancelRunDisposable, _action(_scheduler, _state));
  25. }
  26. }
  27. public IDisposable CancelQueueDisposable
  28. {
  29. //get => Disposable.GetValue(ref _cancelQueueDisposable);
  30. set => Disposable.SetSingle(ref _cancelQueueDisposable, value);
  31. }
  32. public void Dispose()
  33. {
  34. Disposable.TryDispose(ref _cancelQueueDisposable);
  35. Disposable.TryDispose(ref _cancelRunDisposable);
  36. }
  37. }
  38. }