コード例 #1
0
        public async Task IntervalTest()
        {
            var tcs = new TaskCompletionSource<bool>();
            var resetEvent = new AsyncAutoResetEvent();

            //Timeout in 100ms
            Task timeoutTask = resetEvent.WaitAsync(TimeSpan.FromMilliseconds(100)).ContinueWith((r) => tcs.TrySetResult(true));
            Task testTimeoutTask = Task.Delay(1000).ContinueWith((r) => tcs.TrySetResult(false));

            bool success = await tcs.Task;
            Assert.IsTrue(success, "Timeout for async reset event failed to fire");
        }
コード例 #2
0
        public async Task<bool> StartupAsync(Func<object, Task> workerMethod, object workerState, Func<bool> checkForContinueMethod = null)
        {
            await ShutdownAsync();

            if (workerMethod == null)
                throw new ArgumentException("WorkerMethod cannot be null", "workerMethod");

            IsRunning = true;

            this.workerMethod = workerMethod;
            this.workerState = workerState;
            this.checkForContinueMethod = checkForContinueMethod;

            asyncResetEvent = new AsyncAutoResetEvent();
            await Task.Factory.StartNew(() => AsyncWorker(), TaskCreationOptions.LongRunning);
            return true;
        }
コード例 #3
0
        public async Task <bool> StartupAsync(Func <object, Task> workerMethod, object workerState, Func <bool> checkForContinueMethod = null)
        {
            await ShutdownAsync();

            if (workerMethod == null)
            {
                throw new ArgumentException("WorkerMethod cannot be null", "workerMethod");
            }

            IsRunning = true;

            this.workerMethod           = workerMethod;
            this.workerState            = workerState;
            this.checkForContinueMethod = checkForContinueMethod;

            asyncResetEvent = new AsyncAutoResetEvent();
            await Task.Factory.StartNew(() => AsyncWorker(), TaskCreationOptions.LongRunning);

            return(true);
        }
コード例 #4
0
        /// <summary>
        /// Shuts down the auto reset worker.
        /// </summary>
        /// <param name="maxWait">Amount of time, in milliseconds, to wait for a current running task to complete.</param>
        /// <returns>True if shutdown was successful, or false if maxWait elapsed.  A return of false indicates the worker task has not yet completed.</returns>
        public async Task<bool> ShutdownAsync(int maxWait = Timeout.Infinite)
        {
            IsRunning = false;

            bool timedOut = false;
            if (isWorkerRunning)
            {
                if (asyncResetEvent != null)
                {
                    asyncResetEvent.Set();
                }

                DateTime starttime = DateTime.Now;
                while (isWorkerRunning)
                {
                    await Task.Delay(10);

                    if (maxWait != Timeout.Infinite && starttime.AddMilliseconds(maxWait) < DateTime.Now)
                    {
                        timedOut = true;
                        break;
                    }
                }
            }

            workerMethod = null;
            checkForContinueMethod = null;
            asyncResetEvent = null;

            return !timedOut;
        }