예제 #1
0
        public List <AsyncTask> CyclicPartitionFor(int lowerBound, int upperBound, Action <int, CancellationTokenSource, int> action, CancellationTokenSource cancellationSource, bool wait)
        {
            int len         = upperBound - lowerBound + 1;
            int threadCount = Concurrency;
            int partLen     = len / threadCount;

            if (len % threadCount > 0)
            {
                partLen += 1;
            }
            List <AsyncTask> ret = new List <AsyncTask>();

            for (int i = 0; i < threadCount; ++i)
            {
                int start = lowerBound + i;
                int end   = upperBound;
                Action <AsyncTask> actionTask = (task) =>
                {
                    int id = (int)task.DynamicFields["ID"];
                    CyclicParitionRunner(start, end, threadCount, action, id, cancellationSource);
                };
                AsyncTask thread = new AsyncTask(actionTask);
                thread.DynamicFields["ID"] = i;
                thread.Start(false);
                ret.Add(thread);
            }
            WaitForAsyncTasks(ret, wait);
            return(ret);
        }
예제 #2
0
        public void Run()
        {
            AsyncTask task = new AsyncTask(() =>
            {
                TracerBody();
            });

            task.Start(false);
        }
예제 #3
0
        /// <summary>
        /// Test Program
        /// </summary>
        /// <param name="argv"></param>
        public static void Test(String[] argv)
        {
            AsyncTask goodjob = new AsyncTask(new Action(() => { Console.WriteLine("Main Job"); }));

            goodjob.AddAfterFinishJob(new Action(() => { Console.WriteLine("Additional Job"); }));
            goodjob.Start(false);
            goodjob.FlushJob();
            Console.WriteLine("Good Job-Available={0};IsFault={1}", goodjob.IsAvailable, goodjob.IsFault);
            AsyncTask badjob = new AsyncTask(new Action(() => { Console.WriteLine("Main Job2"); throw new Exception("No thing"); }));

            badjob.AddAfterFinishJob(new Action(() => { Console.WriteLine("Additional Job2"); }));
            badjob.Start(true);
            badjob.FlushJob();
            Console.WriteLine("Bad Job-Available={0};IsFault={1}", goodjob.IsAvailable, goodjob.IsFault);
        }
예제 #4
0
        public static TaskContinuable QueueWorkingItem(Action action,
                                                       [CallerMemberName] string memberName    = "",
                                                       [CallerFilePath] string sourceFilePath  = "",
                                                       [CallerLineNumber] int sourceLineNumber = 0)
        {
            AsyncTask task = new AsyncTask(action);

            task.CallerInfo.MemberName = memberName;
            task.CallerInfo.SourceCode = sourceFilePath;
            task.CallerInfo.Line       = sourceLineNumber;
            TaskContinuable continueItem = new TaskContinuable();

            task.DisposeAfterFinish  = true;
            continueItem.CurrentTask = task;
            task.SetName("Async-QueueWorkingItem");
            task.Start(false);
            return(continueItem);
        }
예제 #5
0
        public List <AsyncTask> RuntimeLoadBalanceFor(int lowerBound, int upperBound, Action <int, CancellationTokenSource, int> action, CancellationTokenSource cancellationSource, bool wait)
        {
            List <AsyncTask> ret    = new List <AsyncTask>();
            object           locker = new object();
            int sharedIdx           = lowerBound;
            int threadCount         = Concurrency;

            for (int i = 0; i < threadCount; ++i)
            {
                AsyncTask task = new AsyncTask(() =>
                {
                    int load = lowerBound;
                    while (true)
                    {
                        if (cancellationSource != null && cancellationSource.IsCancellationRequested)
                        {
                            // cancelled
                            break;
                        }
                        // fetch next load
                        lock (locker)
                        {
                            load = sharedIdx;
                            ++sharedIdx;
                        }
                        if (load >= upperBound)
                        {
                            break;
                        }
                        // consume load
                        action(load, cancellationSource, i);
                    }
                });
                task.Start(false);
                ret.Add(task);
            }
            WaitForAsyncTasks(ret, wait);
            return(ret);
        }