Пример #1
0
        // Start new task, independent from acutal task
        private void ForceStart()
        {
            // create new internal task
            var itc = new InternalTaskContainer {
                Cts = new CancellationTokenSource()
            };

            itc.Task = Task.Run(() => SpawnerFunc(itc.Cts.Token)).ContinueWith((t) => TaskEnd(this, t));
            //swap and stop old task
            var oldItc = Interlocked.Exchange(ref _TaskContainer, itc);

            Stop(oldItc, WaitTime);
            // event notifier
            OnTaskStart?.Invoke(this, EventArgs.Empty);
        }
Пример #2
0
        // internal stop/cleanup with timespan
        private static void Stop(InternalTaskContainer itc, TimeSpan waitTime)
        {
            if (itc == null)
            {
                return;
            }

            try
            {
                itc.Cts?.Cancel();
                itc.Task?.Wait(waitTime);
            }
            finally
            {
                itc.Cts?.Dispose();
                itc.Task?.Dispose();
            }
        }