Beispiel #1
0
        public IAsyncResult Run <TResult>(Func <TResult> work, AsyncCallback asyncCallback, IPrincipal principal = null)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("Thread pool has been disposed.");
            }

            if (principal == null)
            {
                principal = Thread.CurrentPrincipal;

                if (Thread.CurrentPrincipal is ClaimsPrincipal claimsPrincipal)
                {
                    if (claimsPrincipal.Identity is ClaimsIdentity claimsIdentity)
                    {
                        var newClaimsIdentity  = new ClaimsIdentity(claimsIdentity.Claims.ToArray(), claimsIdentity.AuthenticationType, claimsIdentity.NameClaimType, claimsIdentity.RoleClaimType);
                        var newClaimsPrincipal = new ClaimsPrincipal(new[] { newClaimsIdentity });
                        principal = newClaimsPrincipal;
                    }
                }
            }

            var asyncResult = new ZerraAsyncResult(asyncCallback);

            lock (workItems)
            {
                workItems.Enqueue(new WorkItem(work, asyncResult, principal));
            }
            checkQueuedWorkItems.Set();
            return(asyncResult);
        }
Beispiel #2
0
            public void ExecuteWorkItem(WorkItem workItem)
            {
                if (disposed)
                {
                    throw new ObjectDisposedException("Worker thread has been disposed.");
                }
                if (!isAvailable)
                {
                    throw new InvalidOperationException("Worker thread is busy");
                }

                principal   = workItem.Principal;
                task        = workItem.Delegate;
                asyncResult = workItem.AsyncResult;
                isAvailable = false;
                startNewWorkItem.Set();
            }
Beispiel #3
0
            private void MainWorkerThread()
            {
                while (true)
                {
                    if (disposed)
                    {
                        return;
                    }
                    startNewWorkItem.WaitOne();
                    if (disposed)
                    {
                        return;
                    }

                    try
                    {
                        Thread.CurrentPrincipal = principal;
                        object result = task.DynamicInvoke(null);
                        if (asyncResult != null)
                        {
                            asyncResult.SetCompleted(result);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (asyncResult != null)
                        {
                            asyncResult.SetCompletedException(ex);
                        }
                    }
                    isAvailable = true;
                    asyncResult = null;
                    task        = null;
                    if (disposed)
                    {
                        return;
                    }
                    workItemComplete.Set();
                }
            }
Beispiel #4
0
 public WorkItem(MulticastDelegate del, ZerraAsyncResult asyncResult, IPrincipal princical)
 {
     this.Delegate    = del;
     this.AsyncResult = asyncResult;
     this.Principal   = princical;
 }