Beispiel #1
0
        public static IEnumerable <CancellationTokenSource> EnumerateLinkedSources(
            this CancellationTokenSource source, bool includeNested = false, bool includeItself = false)
        {
            if (includeItself)
            {
                yield return(source);
            }

            Queue <CancellationTokenSource> nestedSources = null;

            while (source != null)
            {
                var registrations = source.GetLinkingRegistrations();
                source = null;
                if (registrations?.Length > 0)
                {
                    foreach (var registration in registrations)
                    {
                        var callbackInfo = registration.GetCallbackInfo();
                        var linkedSource = CancellationCallbackInfoExtenstions
                                           .GetCancellationTokenSource(callbackInfo);

                        yield return(linkedSource);

                        if (includeNested)
                        {
                            if (source == null)
                            {
                                source = linkedSource;
                            }
                            else
                            {
                                if (nestedSources == null)
                                {
                                    nestedSources = new Queue <CancellationTokenSource>();
                                }
                                nestedSources.Enqueue(linkedSource);
                            }
                        }
                    }
                }

                if (source == null && nestedSources?.Count > 0)
                {
                    source = nestedSources.Dequeue();
                }
            }
        }
Beispiel #2
0
        public static CancellationTokenSource[] GetLinkedSources(this CancellationTokenSource source)
        {
            CancellationTokenSource[] result = null;
            var registrations = source.GetLinkingRegistrations();

            source = null;
            if (registrations?.Length > 0)
            {
                result = new CancellationTokenSource[registrations.Length];
                for (var i = 0; i < registrations.Length; i++)
                {
                    var registration = registrations[i];
                    var callbackInfo = registration.GetCallbackInfo();
                    var linkedSource = CancellationCallbackInfoExtenstions
                                       .GetCancellationTokenSource(callbackInfo);
                    result[i] = linkedSource;
                }
            }
            return(result);
        }