Example #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>
        /// <param name="state">An object containing data to be used by the work.</param>
        public void Invoke(Func <object, CancellationToken, Task> function, object state,
                           InvokeOptions options   = InvokeOptions.Normal,
                           SyncOptions syncOptions = null)
        {
            if (syncOptions != null)
            {
                ValidateSyncOptions(syncOptions);
            }

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

            QueueWork(work);
        }
Example #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>
        /// <param name="state">An object containing data to be used by the work.</param>
        public Task <Task> InvokeAsync(Func <object, CancellationToken, Task> function, object state,
                                       InvokeOptions options   = InvokeOptions.Normal,
                                       SyncOptions syncOptions = null)
        {
            if (syncOptions != null)
            {
                ValidateSyncOptions(syncOptions);
            }

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

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