public void CancellingExecution(AsyncWorker asyncWorker, DoWorkEventHandler worker)
 {
     if (this.log.IsDebugEnabled)
     {
         this.log.Debug(string.Format(
                      CultureInfo.InvariantCulture,
                      "{0} cancels asynchronous operation {1}.{2}()",
                      asyncWorker,
                      worker.Method.DeclaringType.FullName,
                      worker.Method.Name));
     }
 }
 public void StartedExecution(AsyncWorker asyncWorker, DoWorkEventHandler worker, object argument)
 {
     if (this.log.IsDebugEnabled)
     {
         this.log.Debug(string.Format(
                      CultureInfo.InvariantCulture,
                      "{0} executes asynchronous operation {1}.{2}({3})",
                      asyncWorker,
                      worker.Method.DeclaringType.FullName,
                      worker.Method.Name,
                      argument));
     }
 }
Пример #3
0
        public void CanCancelOperation()
        {
            AutoResetEvent workerStarted    = new AutoResetEvent(false);
            AutoResetEvent workerExecuted   = new AutoResetEvent(false);
            AutoResetEvent workerCancelled  = new AutoResetEvent(false);
            AutoResetEvent allowTerminating = new AutoResetEvent(false);

            bool?cancelled = null;

            DoWorkEventHandler worker = delegate(object sender, DoWorkEventArgs e)
            {
                AsyncWorker genericWorker = (AsyncWorker)sender;
                genericWorker.WorkerSupportsCancellation = true;

                workerStarted.Set();
                while (!genericWorker.CancellationPending)
                {
                    Thread.Sleep(1);
                }

                e.Cancel = true;
                workerCancelled.Set();
                allowTerminating.WaitOne();
            };

            RunWorkerCompletedEventHandler completed = delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                cancelled = e.Cancelled;
                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker, completed);

            testee.RunWorkerAsync();
            workerStarted.WaitOne(TimeOut).Should().BeTrue("worker should start.");

            testee.CancelAsync();
            workerCancelled.WaitOne(TimeOut).Should().BeTrue("worker should cancel.");

            allowTerminating.Set();
            workerExecuted.WaitOne(TimeOut).Should().BeTrue("worker should execute.");

            cancelled.Should().BeTrue("result should reflect canceled state.");
        }
Пример #4
0
        public void ExecutesAsyncOperation()
        {
            AutoResetEvent go             = new AutoResetEvent(false);
            AutoResetEvent workerExecuted = new AutoResetEvent(false);

            DoWorkEventHandler worker = delegate
            {
                go.WaitOne();
                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker);

            testee.RunWorkerAsync();
            go.Set();

            var finished = workerExecuted.WaitOne(TimeOut);

            finished.Should().BeTrue("worker should execute.");
        }
Пример #5
0
        public void PassesExceptionThrownByWorkerMethodToGlobalExceptionHandler_WhenNoCompletedHandlerIsSpecified()
        {
            AppDomain.CurrentDomain.UnhandledException += this.UnhandledException;

            DoWorkEventHandler worker = delegate
            {
                throw new InvalidOperationException("test");
            };

            AsyncWorker testee = new AsyncWorker(worker);

            testee.RunWorkerAsync();

            var receivedException = this.caughtExceptionSignal.WaitOne(TimeOut);

            receivedException.Should().BeTrue("exception should be caught");
            this.caughtException.Should().BeOfType <AsyncWorkerException>();

            AppDomain.CurrentDomain.UnhandledException -= this.UnhandledException;
        }
Пример #6
0
        public void ExecutesAsyncOperation()
        {
            AutoResetEvent go = new AutoResetEvent(false);
            AutoResetEvent workerExecuted = new AutoResetEvent(false);

            DoWorkEventHandler worker = delegate
            {
                go.WaitOne();
                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker);

            testee.RunWorkerAsync();
            go.Set();

            var finished = workerExecuted.WaitOne(TimeOut);
                
            finished.Should().BeTrue("worker should execute.");
        }
Пример #7
0
        public void ExecutesAsyncOperationWithArguments()
        {
            AutoResetEvent go             = new AutoResetEvent(false);
            AutoResetEvent workerExecuted = new AutoResetEvent(false);

            const string Argument         = "test";
            string       receivedArgument = null;

            DoWorkEventHandler worker = delegate(object sender, DoWorkEventArgs e)
            {
                receivedArgument = (string)e.Argument;
                go.WaitOne();
                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker);

            testee.RunWorkerAsync(Argument);
            go.Set();

            workerExecuted.WaitOne(TimeOut);

            receivedArgument.Should().Be(Argument);
        }
Пример #8
0
        public void ExecutesAsyncOperationWithArguments()
        {
            AutoResetEvent go = new AutoResetEvent(false);
            AutoResetEvent workerExecuted = new AutoResetEvent(false);

            const string Argument = "test";
            string receivedArgument = null;

            DoWorkEventHandler worker = delegate(object sender, DoWorkEventArgs e)
            {
                receivedArgument = (string)e.Argument;
                go.WaitOne();
                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker);

            testee.RunWorkerAsync(Argument);
            go.Set();

            workerExecuted.WaitOne(TimeOut);

            receivedArgument.Should().Be(Argument);
        }
 /// <summary>
 /// Called when an operation reports progress.
 /// </summary>
 /// <param name="asyncWorker">The asynchronous worker.</param>
 /// <param name="worker">The worker.</param>
 /// <param name="progress">The progress.</param>
 /// <param name="userState">State of the user.</param>
 public void ReportProgress(AsyncWorker asyncWorker, DoWorkEventHandler worker, ProgressChangedEventHandler progress, object userState)
 {
     if (this.log.IsDebugEnabled)
     {
         this.log.Debug(string.Format(
                      CultureInfo.InvariantCulture,
                      "{0} reports progress for operation {1}.{2}()",
                      asyncWorker,
                      worker.Method.DeclaringType.FullName,
                      worker.Method.Name));
     }
 }
 private void LogOperationCompletedWithException(AsyncWorker asyncWorker, DoWorkEventHandler worker, Exception exception)
 {
     this.log.Debug(string.Format(
         CultureInfo.InvariantCulture,
         "{0} completes asynchronous operation {1}.{2}() with exception = {3}",
         asyncWorker,
         worker.Method.DeclaringType.FullName,
         worker.Method.Name,
         exception));
 }
 /// <summary>
 /// Called when an operation was completed.
 /// </summary>
 /// <param name="asyncWorker">The asynchronous worker.</param>
 /// <param name="worker">The worker.</param>
 /// <param name="completed">The completed handler.</param>
 /// <param name="runWorkerCompletedEventArgs">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
 public void CompletedExecution(AsyncWorker asyncWorker, DoWorkEventHandler worker, RunWorkerCompletedEventHandler completed, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs)
 {
     if (this.log.IsDebugEnabled)
     {
         if (runWorkerCompletedEventArgs.Error == null)
         {
             this.LogOperationCompletedWithoutException(asyncWorker, worker);
         }
         else
         {
             this.LogOperationCompletedWithException(asyncWorker, worker, runWorkerCompletedEventArgs.Error);
         }
     }
 }
Пример #12
0
        public void ExecutesCompletedHandler()
        {
            AutoResetEvent workerExecuted = new AutoResetEvent(false);

            const int Result = 3;
            Exception receivedException = new Exception();
            int receivedResult = 0;

            DoWorkEventHandler worker = delegate(object sender, DoWorkEventArgs e)
            {
                e.Result = Result;
            };

            RunWorkerCompletedEventHandler completed = delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                receivedException = e.Error;
                receivedResult = (int)e.Result;

                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker, completed);

            testee.RunWorkerAsync();

            workerExecuted.WaitOne(TimeOut);

            receivedException.Should().BeNull();
            receivedResult.Should().Be(Result);
        }
Пример #13
0
        public void CanNotifyAboutProgress()
        {
            AutoResetEvent workerExecuted = new AutoResetEvent(false);

            DoWorkEventHandler worker = delegate(object sender, DoWorkEventArgs e)
            {
                AsyncWorker genericWorker = (AsyncWorker)sender;
                genericWorker.WorkerReportsProgress = true;

                for (int i = 0; i <= 100; i += 10)
                {
                    genericWorker.ReportProgress(i, null);
                }
            };

            int count = 0;
            ProgressChangedEventHandler progress = (sender, e) =>
            {
                count++;
                if (count == 10)
                {
                    workerExecuted.Set();
                }
            };

            AsyncWorker testee = new AsyncWorker(worker, progress, null);

            testee.RunWorkerAsync();

            workerExecuted.WaitOne(TimeOut).Should().BeTrue();
        }
Пример #14
0
        public void CanCancelOperation()
        {
            AutoResetEvent workerStarted = new AutoResetEvent(false);
            AutoResetEvent workerExecuted = new AutoResetEvent(false);
            AutoResetEvent workerCancelled = new AutoResetEvent(false);
            AutoResetEvent allowTerminating = new AutoResetEvent(false);

            bool? cancelled = null;

            DoWorkEventHandler worker = delegate(object sender, DoWorkEventArgs e)
            {
                AsyncWorker genericWorker = (AsyncWorker)sender;
                genericWorker.WorkerSupportsCancellation = true;

                workerStarted.Set();
                while (!genericWorker.CancellationPending)
                {
                    Thread.Sleep(1);
                }

                e.Cancel = true;
                workerCancelled.Set();
                allowTerminating.WaitOne();
            };

            RunWorkerCompletedEventHandler completed = delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                cancelled = e.Cancelled;
                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker, completed);

            testee.RunWorkerAsync();
            workerStarted.WaitOne(TimeOut).Should().BeTrue("worker should start.");

            testee.CancelAsync();
            workerCancelled.WaitOne(TimeOut).Should().BeTrue("worker should cancel.");

            allowTerminating.Set();
            workerExecuted.WaitOne(TimeOut).Should().BeTrue("worker should execute.");

            cancelled.Should().BeTrue("result should reflect canceled state.");
        }
Пример #15
0
        public void PassesExceptionThrownByWorkerMethodToCompletedEventHandler()
        {
            AppDomain.CurrentDomain.UnhandledException += this.UnhandledException;

            AutoResetEvent workerExecuted = new AutoResetEvent(false);
            var exception = new InvalidOperationException("test");
            Exception receivedException = null;

            DoWorkEventHandler worker = delegate
                {
                    throw exception;
                };

            RunWorkerCompletedEventHandler completed = delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                receivedException = e.Error;
                workerExecuted.Set();
            };

            AsyncWorker testee = new AsyncWorker(worker, completed);

            testee.RunWorkerAsync();

            workerExecuted.WaitOne(TimeOut);

            this.caughtException.Should().BeNull("no exception should be handled globally.");
            receivedException.Should().BeSameAs(exception);

            AppDomain.CurrentDomain.UnhandledException -= this.UnhandledException;
        }
Пример #16
0
        public void PassesExceptionThrownByWorkerMethodToGlobalExceptionHandler_WhenNoCompletedHandlerIsSpecified()
        {
            AppDomain.CurrentDomain.UnhandledException += this.UnhandledException;

            DoWorkEventHandler worker = delegate
            {
                throw new InvalidOperationException("test");
            };

            AsyncWorker testee = new AsyncWorker(worker);

            testee.RunWorkerAsync();

            var receivedException = this.caughtExceptionSignal.WaitOne(TimeOut);
            
            receivedException.Should().BeTrue("exception should be caught");
            this.caughtException.Should().BeOfType<AsyncWorkerException>();

            AppDomain.CurrentDomain.UnhandledException -= this.UnhandledException;
        }