コード例 #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));
        }
コード例 #2
0
 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;
     }
 }
コード例 #3
0
        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;
        }
コード例 #4
0
 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();
     }
 }
コード例 #5
0
 public static Task RunAsync(this IEnumerator that, TaskDistributor target)
 {
     return(target.Dispatch(Task.Create(that)));
 }
コード例 #6
0
        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);
        }
コード例 #7
0
 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));
 }
コード例 #8
0
        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);
        }
コード例 #9
0
 public static Task <T> RunAsync <T>(this Func <T> that, TaskDistributor target)
 {
     return(target.Dispatch(that));
 }
コード例 #10
0
 public static Task RunAsync(this Action that, TaskDistributor target)
 {
     return(target.Dispatch(that));
 }
コード例 #11
0
 private void EnsureHelperInstance()
 {
     dispatcher      = Dispatcher.MainNoThrow ?? new Dispatcher();
     taskDistributor = TaskDistributor.MainNoThrow ?? new TaskDistributor("TaskDistributor");
 }
コード例 #12
0
ファイル: TaskWorker.cs プロジェクト: smdx24/CPI-Source-Code
 public TaskWorker(string name, TaskDistributor taskDistributor)
     : base(name, autoStartThread: false)
 {
     TaskDistributor = taskDistributor;
     Dispatcher      = new Dispatcher(setThreadDefaults: false);
 }