Exemple #1
0
 private Task processTask(MyTask myTask)
 {
     _semaphoreSlim.Wait();
     return(Task.Run(async() =>
     {
         await Task.Run(() => myTask.DoStepN(1)).ConfigureAwait(false);
         await Task.Run(() => myTask.DoStepN(2)).ConfigureAwait(false);
         await Task.Run(() => myTask.DoStepN(3)).ConfigureAwait(false);
         _semaphoreSlim.Release();
     }));
 }
        private Task processTask(MyTask myTask)
        {
            CancellationToken cts = new CancellationToken();

            return(Task.Run(async() =>
            {
                await Task.Factory.StartNew(() => myTask.DoStepN(1), cts,
                                            TaskCreationOptions.None, step1.ConcurrentScheduler)
                .ContinueWith(x => myTask.DoStepN(2), step2.ConcurrentScheduler)
                .ContinueWith(x => myTask.DoStepN(3), step3.ConcurrentScheduler)
                ;
            }));
        }
            public JobWorker(int whichStep, int threadLimit, int toProcess, ManualResetEvent resetEvent)
            {
                semaphore = new Semaphore(threadLimit, threadLimit);

                while (true)
                {
                    //Stopwatch stopwatch = new Stopwatch();
                    //stopwatch.Start();
                    MyTask task = Queue.Poll(whichStep);
                    //stopwatch.Stop();
                    //Console.WriteLine(whichStep + " poll..." + stopwatch.ElapsedMilliseconds);

                    semaphore.WaitOne();

                    Task.Run(() =>
                    {
                        task.DoStepN(whichStep);

                        semaphore.Release();

                        if (Interlocked.Decrement(ref toProcess) == 0)
                        {
                            resetEvent.Set();
                        }
                    });
                }
            }
        private void ThreadBody()
        {
            while (true)
            {
                while (_queue.Count > 0)
                {
                    MyTask task = null;
                    lock (_queue)
                    {
                        _queue.TryDequeue(out task);
                    }

                    if (task != null)
                    {
                        task.DoStepN(_step);
                        Console.WriteLine($"pipeline_{_step} 執行完 Task: {task.ID}");
                        if (NextPipeline != null)
                        {
                            NextPipeline.AddTask(task);
                        }
                    }
                }

                if (this._finished)
                {
                    Console.WriteLine($"Thread_{Thread.CurrentThread.ManagedThreadId} close");
                    break;
                }
                else
                {
                    _mre.WaitOne();
                }
            }
        }
Exemple #5
0
        private void DoWorkerThread()
        {
            while (true)
            {
                while (this._workitems.Count > 0)
                {
                    MyTask task = null;
                    lock (this._workitems)
                    {
                        if (this._workitems.Count > 0)
                        {
                            task = this._workitems.Dequeue();
                        }
                    }

                    if (task == null)
                    {
                        continue;
                    }

                    task.DoStepN(1);
                    task.DoStepN(2);
                    task.DoStepN(3);

                    if (this._cancel_flag == true)
                    {
                        break;
                    }
                }

                if (this._stop_flag == true || this._cancel_flag == true)
                {
                    break;
                }
                if (this.enqueueNotify.WaitOne(this._maxWorkerThreadTimeout, true) == true)
                {
                    continue;
                }
                break;
            }

            this._workerThreads.Remove(Thread.CurrentThread);
        }
            public JobWorker(int whichStep, int threadLimit)
            {
                semaphore = new Semaphore(threadLimit, threadLimit);

                while (true)
                {
                    //Stopwatch stopwatch = new Stopwatch();
                    //stopwatch.Start();
                    MyTask task = Queue.Poll(whichStep);
                    //stopwatch.Stop();
                    //Console.WriteLine(whichStep + " poll..." + stopwatch.ElapsedMilliseconds);

                    semaphore.WaitOne();

                    Task.Run(() =>
                    {
                        task.DoStepN(whichStep);

                        Queue.Produce(task.CurrentStep + 1, task);

                        semaphore.Release();
                    });
                }
            }
        public override void Run(IEnumerable <MyTask> tasks)
        {
            int maxConcurentTasks = 11 + 3;

            ThreadPool.SetMinThreads(maxConcurentTasks, maxConcurentTasks);
            ThreadPool.SetMaxThreads(maxConcurentTasks, maxConcurentTasks);
            int totalToProcess = 0;

            int toProcess = 0;

            int bc2Count = 0;

            int bc3Count = 0;

            BlockingCollection <MyTask> bc1 = new BlockingCollection <MyTask>();

            BlockingCollection <MyTask> bc2 = new BlockingCollection <MyTask>();

            BlockingCollection <MyTask> bc3 = new BlockingCollection <MyTask>();

            Semaphore semaphore1 = new Semaphore(5, 5);
            Semaphore semaphore2 = new Semaphore(3, 3);
            Semaphore semaphore3 = new Semaphore(3, 3);

            foreach (MyTask task in tasks)
            {
                bc1.Add(task);

                Interlocked.Increment(ref toProcess);
            }
            totalToProcess = toProcess;

            bc1.CompleteAdding();

            using (ManualResetEvent resetEvent = new ManualResetEvent(false))
            {
                Task bc1Runner = Task.Run(() =>
                {
                    while (true)
                    {
                        MyTask task = bc1.Take();
                        semaphore1.WaitOne();
                        Task.Run(() =>
                        {
                            {
                                task.DoStepN(1);
                                Console.WriteLine("do " + task.ID + " dostep 1...END");
                                bc2.Add(task);
                                Interlocked.Increment(ref bc2Count);
                            }
                            semaphore1.Release();
                        });
                    }
                    Console.WriteLine("bc1 complete!!!!!!");
                });


                Task bc2Runner = Task.Run(() =>
                {
                    while (true)
                    {
                        if (bc2Count == totalToProcess)
                        {
                            bc2.CompleteAdding();
                        }

                        Console.WriteLine(bc2.Count);

                        MyTask task = bc2.Take();

                        semaphore2.WaitOne();
                        Task.Run(() =>
                        {
                            {
                                task.DoStepN(2);
                                Console.WriteLine("do " + task.ID + " dostep 2...END");
                                bc3.Add(task);
                                Interlocked.Increment(ref bc3Count);
                            }
                            semaphore2.Release();
                        });
                    }
                    Console.WriteLine("bc2 complete!!!!!!");
                });


                Task task3Runner = Task.Run(() =>
                {
                    while (true)
                    {
                        if (bc3Count == totalToProcess)
                        {
                            bc3.CompleteAdding();
                        }

                        MyTask task = bc3.Take();

                        semaphore3.WaitOne();
                        Task.Run(() =>
                        {
                            {
                                task.DoStepN(3);
                                Console.WriteLine(task.ID + " dostep 3...");

                                if (Interlocked.Decrement(ref toProcess) == 0)
                                {
                                    resetEvent.Set();
                                }
                            }
                            semaphore3.Release();
                        });
                    }
                    Console.WriteLine("bc3 complete!!!!!!");
                });


                resetEvent.WaitOne();
            }
        }
 public void DoStepN(int step)
 {
     _myTask.DoStepN(step);
     doing = false;
 }
        public override void Run(IEnumerable <MyTask> tasks)
        {
            int maxConcurentTasks = 11 + 6;

            ThreadPool.SetMinThreads(maxConcurentTasks, maxConcurentTasks);
            ThreadPool.SetMaxThreads(maxConcurentTasks, maxConcurentTasks);
            int totalToProcess = 0;

            int toProcess = 0;

            int bc2Count = 0;

            int bc3Count = 0;

            BlockingCollection <MyTask> bc1 = new BlockingCollection <MyTask>();

            BlockingCollection <MyTask> bc2 = new BlockingCollection <MyTask>();

            BlockingCollection <MyTask> bc3 = new BlockingCollection <MyTask>();

            foreach (MyTask task in tasks)
            {
                bc1.Add(task);

                Interlocked.Increment(ref toProcess);
            }
            totalToProcess = toProcess;

            bc1.CompleteAdding();

            using (ManualResetEvent resetEvent = new ManualResetEvent(false))
            {
                Task bc1Runner = Task.Run(() =>
                {
                    while (!bc1.IsCompleted)
                    {
                        MyTask task = bc1.Take();

                        Task.Run(async() =>
                        {
                            await Task.Factory.StartNew(() =>
                            {
                                task.DoStepN(1);
                            });


                            Console.WriteLine(task.ID + " dostep 1 " + DateTime.Now + "...finish");
                            bc2.Add(task);
                            Interlocked.Increment(ref bc2Count);
                        });

                        //Task.Factory.StartNew(() =>
                        //{
                        //    task.DoStepN(1);

                        //    Console.WriteLine(task.ID + " dostep 1...finish");
                        //    bc2.Add(task);
                        //    Interlocked.Increment(ref bc2Count);
                        //});

                        //ThreadPool.QueueUserWorkItem(new WaitCallback(x =>
                        //{
                        //    ((MyTask)x).DoStepN(1);

                        //    Console.WriteLine(((MyTask)x).ID + " dostep 1...finish");
                        //    bc2.Add(((MyTask)x));
                        //    Interlocked.Increment(ref bc2Count);
                        //}), task);
                    }
                    Console.WriteLine("bc1 complete!!!!!!");
                });


                Task bc2Runner = Task.Run(() =>
                {
                    while (!bc2.IsCompleted)
                    {
                        if (bc2Count == totalToProcess)
                        {
                            bc2.CompleteAdding();
                        }

                        Console.WriteLine(bc2.Count);

                        MyTask task = bc2.Take();

                        Task.Run(async() =>
                        {
                            await Task.Factory.StartNew(() =>
                            {
                                task.DoStepN(2);
                            });

                            Console.WriteLine("do " + task.ID + " dostep 2...");
                            bc3.Add(task);
                            Interlocked.Increment(ref bc3Count);
                        });

                        //Task.Factory.StartNew(() =>
                        //{
                        //    Console.WriteLine("do " + task.ID + " dostep 2...");
                        //    task.DoStepN(2);

                        //    bc3.Add(task);
                        //    Interlocked.Increment(ref bc3Count);
                        //});

                        //ThreadPool.QueueUserWorkItem(new WaitCallback(x =>
                        //{
                        //    Console.WriteLine("do " + ((MyTask)x).ID + " dostep 2...");
                        //    ((MyTask)x).DoStepN(2);

                        //    bc3.Add(((MyTask)x));
                        //    Interlocked.Increment(ref bc3Count);
                        //}), task);
                    }
                    Console.WriteLine("bc2 complete!!!!!!");
                });


                Task task3Runner = Task.Run(() =>
                {
                    while (!bc3.IsCompleted)
                    {
                        if (bc3Count == totalToProcess)
                        {
                            bc3.CompleteAdding();
                        }

                        MyTask task = bc3.Take();

                        Task.Run(async() =>
                        {
                            await Task.Factory.StartNew(() =>
                            {
                                task.DoStepN(3);
                            });

                            Console.WriteLine(task.ID + " dostep 3...");

                            if (Interlocked.Decrement(ref toProcess) == 0)
                            {
                                resetEvent.Set();
                            }
                        });
                        //Task.Factory.StartNew(() =>
                        //{
                        //    task.DoStepN(3);
                        //    Console.WriteLine(task.ID + " dostep 3...");

                        //    if (Interlocked.Decrement(ref toProcess) == 0)
                        //        resetEvent.Set();
                        //});
                        //ThreadPool.QueueUserWorkItem(new WaitCallback(x =>
                        //{
                        //    ((MyTask)x).DoStepN(3);
                        //    Console.WriteLine(((MyTask)x).ID + " dostep 3...");

                        //    if (Interlocked.Decrement(ref toProcess) == 0)
                        //        resetEvent.Set();
                        //}), task);
                    }
                    Console.WriteLine("bc3 complete!!!!!!");
                });


                resetEvent.WaitOne();
            }
        }