Signals to CancellationTokens that they should be canceled.
Unlike the built-in CancellationToken type of .NET the NanoByte.Common variant supports remoting.
Inheritance: MarshalNoTimeout, IDisposable
Exemple #1
0
        public void TestCancel()
        {
            // Prepare a very slow download of the file and monitor for a cancellation exception
            Server.Slow = true;
            var  download                = new DownloadFile(Server.FileUri, _tempFile);
            bool exceptionThrown         = false;
            var  cancellationTokenSource = new CancellationTokenSource();
            var  downloadThread          = new Thread(() =>
            {
                try
                {
                    download.Run(cancellationTokenSource.Token);
                }
                catch (OperationCanceledException)
                {
                    exceptionThrown = true;
                }
            });

            // Start and then cancel the download
            downloadThread.Start();
            Thread.Sleep(100);
            cancellationTokenSource.Cancel();
            downloadThread.Join();

            exceptionThrown.Should().BeTrue(because: "Should throw OperationCanceledException");
        }
Exemple #2
0
        public void TestCancel()
        {
            // Prepare a very slow download of the file and monitor for a cancellation exception
            Server.Slow = true;
            var download = new DownloadFile(Server.FileUri, _tempFile);
            bool exceptionThrown = false;
            var cancellationTokenSource = new CancellationTokenSource();
            var downloadThread = new Thread(() =>
            {
                try
                {
                    download.Run(cancellationTokenSource.Token);
                }
                catch (OperationCanceledException)
                {
                    exceptionThrown = true;
                }
            });

            // Start and then cancel the download
            downloadThread.Start();
            Thread.Sleep(100);
            cancellationTokenSource.Cancel();
            downloadThread.Join();

            exceptionThrown.Should().BeTrue(because: "Should throw OperationCanceledException");
        }
Exemple #3
0
        /// <summary>
        /// Creates a new progress tracking form.
        /// </summary>
        /// <param name="cancellationTokenSource">Used to signal when the user wishes to cancel the current process.</param>
        public ProgressForm([NotNull] CancellationTokenSource cancellationTokenSource)
        {
            #region Sanity checks
            if (cancellationTokenSource == null)
            {
                throw new ArgumentNullException(nameof(cancellationTokenSource));
            }
            #endregion

            _cancellationTokenSource = cancellationTokenSource;

            InitializeComponent();

            buttonModifySelectionsDone.Text = Resources.Done;
            buttonHide.Text   = Resources.Hide;
            buttonCancel.Text = Resources.Cancel;

            // ReSharper disable once DoNotCallOverridableMethodsInConstructor
            if (Locations.IsPortable)
            {
                Text += @" - " + Resources.PortableMode;
            }

            Shown += delegate { this.SetForegroundWindow(); };
        }
        internal CancellationTokenRegistration(CancellationTokenSource source, Action callback)
        {
            _source = source;
            _callback = callback;

            if (_source != null) _source.CancellationRequested += _callback;
        }
Exemple #5
0
        public MainForm([NotNull] CancellationTokenSource cancellationTokenSource)
        {
            _cancellationTokenSource = cancellationTokenSource;
            InitializeComponent();

            switch (EmbeddedConfig.Instance.AppMode)
            {
                case BootstrapMode.Run:
                    labelLoading.Text = $"Preparing to run {EmbeddedConfig.Instance.AppName}...";
                    break;

                case BootstrapMode.Integrate:
                    labelLoading.Text = $"Preparing to integrate {EmbeddedConfig.Instance.AppName}...";
                    break;
            }

            HandleCreated += delegate
            {
                this.EnableWindowDrag();
                labelBorder.EnableWindowDrag();
                pictureBoxLogo.EnableWindowDrag();
            };
        }