Exemple #1
0
        public static Task Then(this Task that, Task followingTask)
        {
            TaskDistributor target = null;

            if (ThreadBase.CurrentThread is TaskWorker)
            {
                target = ((TaskWorker)ThreadBase.CurrentThread).TaskDistributor;
            }
            return(that.Then(followingTask, target));
        }
 private void OnDestroy()
 {
     foreach (ThreadBase registeredThread in registeredThreads)
     {
         registeredThread.Dispose();
     }
     if (dispatcher != null)
     {
         dispatcher.Dispose();
     }
     dispatcher = null;
     if (taskDistributor != null)
     {
         taskDistributor.Dispose();
     }
     taskDistributor = null;
     if (instance == this)
     {
         instance = null;
     }
 }
        public override void Dispose()
        {
            if (isDisposed)
            {
                return;
            }
            while (true)
            {
                bool flag = true;
                Task task;
                lock (taskListSyncRoot)
                {
                    if (taskList.Count != 0)
                    {
                        task = taskList.Dequeue();
                        goto IL_0052;
                    }
                }
                break;
IL_0052:
                task.Dispose();
            }
            lock (workerThreads)
            {
                for (int i = 0; i < workerThreads.Length; i++)
                {
                    workerThreads[i].Dispose();
                }
                workerThreads = new TaskWorker[0];
            }
            dataEvent.Close();
            dataEvent = null;
            if (mainTaskDistributor == this)
            {
                mainTaskDistributor = null;
            }
            isDisposed = true;
        }
 public TaskDistributor(string name, int workerThreadCount, bool autoStart)
 {
     this.name = name;
     if (workerThreadCount <= 0)
     {
         workerThreadCount = ThreadBase.AvailableProcessors * 2;
     }
     workerThreads = new TaskWorker[workerThreadCount];
     lock (workerThreads)
     {
         for (int i = 0; i < workerThreadCount; i++)
         {
             workerThreads[i] = new TaskWorker(name, this);
         }
     }
     if (mainTaskDistributor == null)
     {
         mainTaskDistributor = this;
     }
     if (autoStart)
     {
         Start();
     }
 }
 public static Task RunAsync(this IEnumerator that, TaskDistributor target)
 {
     return(target.Dispatch(Task.Create(that)));
 }
        public static IEnumerable <Task <TResult> > SequentialForEach <TResult, T>(this IEnumerable <T> that, Func <T, TResult> action, TaskDistributor target)
        {
            List <Task <TResult> > list = new List <Task <TResult> >();
            Task task2 = null;

            foreach (T item in that)
            {
                T tmp = item;
                Task <TResult> task = Task.Create(() => action(tmp));
                if (task2 == null)
                {
                    task.Run(target);
                }
                else
                {
                    task2.WhenEnded((Action) delegate
                    {
                        task.Run(target);
                    });
                }
                task2 = task;
                list.Add(task);
            }
            return(list);
        }
 public static IEnumerable <Task> SequentialForEach <T>(this IEnumerable <T> that, Action <T> action, TaskDistributor target)
 {
     return((IEnumerable <Task>) that.SequentialForEach(delegate(T element)
     {
         action(element);
         return default(Task.Unit);
     }, target));
 }
        public static IEnumerable <Task <TResult> > ParallelForEach <TResult, T>(this IEnumerable <T> that, Func <T, TResult> action, TaskDistributor target)
        {
            List <Task <TResult> > list = new List <Task <TResult> >();

            foreach (T item2 in that)
            {
                T tmp = item2;
                Task <TResult> item = Task.Create(() => action(tmp)).Run(target);
                list.Add(item);
            }
            return(list);
        }
 public static Task <T> RunAsync <T>(this Func <T> that, TaskDistributor target)
 {
     return(target.Dispatch(that));
 }
 public static Task RunAsync(this Action that, TaskDistributor target)
 {
     return(target.Dispatch(that));
 }
 private void EnsureHelperInstance()
 {
     dispatcher      = Dispatcher.MainNoThrow ?? new Dispatcher();
     taskDistributor = TaskDistributor.MainNoThrow ?? new TaskDistributor("TaskDistributor");
 }
Exemple #12
0
 public TaskWorker(string name, TaskDistributor taskDistributor)
     : base(name, autoStartThread: false)
 {
     TaskDistributor = taskDistributor;
     Dispatcher      = new Dispatcher(setThreadDefaults: false);
 }