Beispiel #1
0
 public TaskExecutionState(TState actionState, int parentThreadId, ThreadStackTrace threadStackTrace, CancellationToken token)
 {
     this.ActionState      = actionState;
     this.ParentThreadId   = parentThreadId;
     this.ThreadStackTrace = threadStackTrace;
     this.Token            = token;
 }
Beispiel #2
0
        public void Execute <TState>(Action <TState> action, TState actionState)
            where TState : new()
        {
            void ActionWrapper(TaskExecutionState <TState> scopedTaskState)
            {
                var currentThreadId             = Thread.CurrentThread.ManagedThreadId;
                var canRegisterParentStacktrace = currentThreadId != scopedTaskState.ParentThreadId;

                try
                {
                    if (canRegisterParentStacktrace)
                    {
                        ThreadStackTrace.RegisterParentThreadStackTrace(currentThreadId, scopedTaskState.ThreadStackTrace);
                    }

                    action(scopedTaskState.ActionState);
                }
                finally
                {
                    if (canRegisterParentStacktrace)
                    {
                        ThreadStackTrace.UnregisterParentThreadStackTrace(currentThreadId);
                    }
                }
            }

            this._cts.Token.ThrowIfCancellationRequested();
            var stackedStackTrace = ThreadStackTrace.GetCurrentThreadStackTrace();
            var taskState         = new TaskExecutionState <TState>(actionState, Thread.CurrentThread.ManagedThreadId, stackedStackTrace, this._cts.Token);

            this._taskFactory.StartNew(ActionWrapper, taskState, this._taskFactory.CreationOptions);
        }
        public static bool TryGetParentThreadStackTrace(int threadId, out ThreadStackTrace parentThreadStackTrace)
        {
            if (ParentThreadStackTraces.TryGetValue(threadId, out var stackTrace))
            {
                parentThreadStackTrace = stackTrace;
                return(true);
            }

            parentThreadStackTrace = null;
            return(false);
        }
        public override string ToString()
        {
            var currentThreadId = Thread.CurrentThread.ManagedThreadId;

            var stackTraceText = base.ToString();

            if (ThreadStackTrace.TryGetParentThreadStackTrace(currentThreadId, out var parentThreadStackTrace))
            {
                return("Current thread #" + currentThreadId.ToString(CultureInfo.InvariantCulture) + ":" + Environment.NewLine + stackTraceText + Environment.NewLine + parentThreadStackTrace);
            }

            return(stackTraceText);
        }
Beispiel #5
0
        public static Thread Create(ThreadStart start, ThreadCreationOptions options)
        {
            var thread = new Thread(WrapWithParentStackTraceRemoval(start))
            {
                IsBackground = options.IsBackground,
                Name         = options.Name
            };

            var parentThreadStackTrace = ThreadStackTrace.GetCurrentThreadStackTrace();

            ThreadStackTrace.RegisterParentThreadStackTrace(thread.ManagedThreadId, parentThreadStackTrace);

            return(thread);
        }
Beispiel #6
0
        private static ThreadStart WrapWithParentStackTraceRemoval(ThreadStart start)
        {
            void Wrapper()
            {
                try
                {
                    start();
                }
                finally
                {
                    ThreadStackTrace.UnregisterParentThreadStackTrace(Thread.CurrentThread.ManagedThreadId);
                }
            }

            return(Wrapper);
        }
 public static void RegisterParentThreadStackTrace(int threadId, ThreadStackTrace parentThreadStackTrace)
 {
     ParentThreadStackTraces.TryAdd(threadId, parentThreadStackTrace);
 }
 private ThreadStackTrace(ThreadStackTrace other)
 {
     this._stackTraces = new List <StackTrace>(other._stackTraces);
     this._threadIds   = new List <int>(other._threadIds);
 }