Beispiel #1
0
        /// <summary>
        /// Queues a method for execution. The method executes when this worker is ready.
        /// </summary>
        /// <param name="function">The work to execute asynchronously. CancellationToken will be set when worker is closed.</param>
        public void Invoke(Func <CancellationToken, Task> function,
                           InvokeOptions options   = InvokeOptions.Normal,
                           SyncOptions syncOptions = null)
        {
            if (syncOptions != null)
            {
                ValidateSyncOptions(syncOptions);
            }

            var work = new WorkFt
            {
                Function = function,
                Token    = CreateCancellationToken(),
                Options  = (WorkOptions)((int)options),
                Sync     = syncOptions != null ? new WorkSyncContext(syncOptions) : null
            };

            QueueWork(work);
        }
Beispiel #2
0
        /// <summary>
        /// Queues the specified work to run when this worker is ready and returns a proxy for the task returned by function.
        /// </summary>
        /// <param name="function">The work to execute asynchronously. CancellationToken will be set when worker is closed.</param>
        public Task <Task> InvokeAsync(Func <CancellationToken, Task> function,
                                       InvokeOptions options   = InvokeOptions.Normal,
                                       SyncOptions syncOptions = null)
        {
            if (syncOptions != null)
            {
                ValidateSyncOptions(syncOptions);
            }

            var work = new WorkFt
            {
                Function         = function,
                Token            = CreateCancellationToken(),
                Options          = (WorkOptions)((int)options),
                CompletionSource = new TaskCompletionSource <Task>(),
                Sync             = syncOptions != null ? new WorkSyncContext(syncOptions) : null
            };

            QueueWork(work);
            return(work.CompletionSource.Task);
        }