Example #1
0
        public void CancelTest() {

            var task = new Monitor();
            var count = 0;
            task.Execute(token => {              
                while (count != 100) {
                    count++;
                    token.ThrowIfCancellationRequested();
                    Thread.Sleep(100);
                }
            });

            Assert.True(task.IsRunning);

            task.Cancel();

            Assert.True(task.IsCanceled);
            Assert.False(task.IsRunning);

            Assert.AreNotEqual(100, count);
        }
Example #2
0
        public void DefaultTest() {

            var messages = 0;
            var warnings = 0;
            var exceptions = 0;
            var task = new Monitor();

            task.Message += (sender, args) => {
                messages++;
            };
            task.Exception += (sender, args) => {
                exceptions++;
            };
            task.Warning += (sender, args) => {
                warnings++;
            };

            task.Execute(token => {
                task.OnMessage("m1");
                task.OnMessage("m2");
                task.OnMessage("m3");

                Thread.Sleep(200);

                task.OnWarning("w1");
                task.OnWarning("w2");

                task.OnException(new Exception());

            });

            Assert.AreEqual(true, task.IsRunning);
            task.Wait();

            Assert.AreEqual(3, messages);
            Assert.AreEqual(2, warnings);
            Assert.AreEqual(1, exceptions);
        }