Beispiel #1
0
 public void RunLoop(CancellationToken cancellationToken)
 {
     if (cancellationToken.CanBeCanceled == false)
     {
         _native.RunLoop(null);
     }
     else
     {
         var l            = new object();
         var cancellation = _native.CreateLoopCancellation();
         cancellationToken.Register(() =>
         {
             lock (l)
             {
                 cancellation?.Cancel();
                 cancellation?.Dispose();
                 cancellation = null;
             }
         });
         try
         {
             _native.RunLoop(cancellation);
         }
         finally
         {
             lock (l)
             {
                 cancellation?.Dispose();
                 cancellation = null;
             }
         }
     }
 }
        public void RunLoop(CancellationToken cancellationToken)
        {
            _exceptionDispatchInfo?.Throw();
            var l = new object();

            _exceptionCancellationSource = new CancellationTokenSource();

            var compositeCancellation = CancellationTokenSource
                                        .CreateLinkedTokenSource(cancellationToken, _exceptionCancellationSource.Token).Token;

            var cancellation = _native.CreateLoopCancellation();

            compositeCancellation.Register(() =>
            {
                lock (l)
                {
                    cancellation?.Cancel();
                }
            });

            try
            {
                _native.RunLoop(cancellation);
            }
            finally
            {
                lock (l)
                {
                    cancellation?.Dispose();
                    cancellation = null;
                }
            }

            if (_exceptionDispatchInfo != null)
            {
                _exceptionDispatchInfo.Throw();
            }
        }