Pause and Resume Subscription on cold IObservable

后端 未结 4 511
南笙
南笙 2020-12-03 03:45

Using Rx, I desire pause and resume functionality in the following code:

How to implement Pause() and Resume() ?

    static IDisposable _subscripti         


        
4条回答
  •  我在风中等你
    2020-12-03 04:23

    It just works:

        class SimpleWaitPulse
        {
          static readonly object _locker = new object();
          static bool _go;
    
          static void Main()
          {                                // The new thread will block
            new Thread (Work).Start();     // because _go==false.
    
            Console.ReadLine();            // Wait for user to hit Enter
    
            lock (_locker)                 // Let's now wake up the thread by
            {                              // setting _go=true and pulsing.
              _go = true;
              Monitor.Pulse (_locker);
            }
          }
    
          static void Work()
          {
            lock (_locker)
              while (!_go)
                Monitor.Wait (_locker);    // Lock is released while we’re waiting
    
            Console.WriteLine ("Woken!!!");
          }
        }
    

    Please, see How to Use Wait and Pulse for more details

提交回复
热议问题