當前位置: 首頁>>代碼示例>>C#>>正文


C# Barrier.AddParticipant方法代碼示例

本文整理匯總了C#中System.Threading.Barrier.AddParticipant方法的典型用法代碼示例。如果您正苦於以下問題:C# Barrier.AddParticipant方法的具體用法?C# Barrier.AddParticipant怎麽用?C# Barrier.AddParticipant使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Threading.Barrier的用法示例。


在下文中一共展示了Barrier.AddParticipant方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: BarrierLimitedModeCharacterCounter

        public BarrierLimitedModeCharacterCounter(ITextFile textFile, Barrier barrier)
        {
            this.barrier = barrier;
            barrier.AddParticipant();

            charCounts = new Lazy<IReadOnlyDictionary<char, int>>(() => BufferAndCount(textFile));
        }
開發者ID:peterchase,項目名稱:parallel-workshop,代碼行數:7,代碼來源:BarrierLimitedModeCharacterCounter.cs

示例2: Main

        static void Main()
        {
            const int numberTasks = 2;
            const int partitionSize = 1000000;
            const int loops = 5;
            var taskResults = new Dictionary<int, int[][]>();
            var data = new List<string>[loops];
            for (int i = 0; i < loops; i++)
            {
                data[i] = new List<string>(FillData(partitionSize * numberTasks));
            }

            var barrier = new Barrier(1);
            LogBarrierInformation("initial participants in barrier", barrier);

            for (int i = 0; i < numberTasks; i++)
            {
                barrier.AddParticipant();

                int jobNumber = i;
                taskResults.Add(i, new int[loops][]);
                for (int loop = 0; loop < loops; loop++)
                {
                    taskResults[i][loop] = new int[26];
                }
                WriteLine($"Main - starting task job {jobNumber}");
                Task.Run(() => CalculationInTask(jobNumber, partitionSize, barrier, data, loops, taskResults[jobNumber]));
            }

            for (int loop = 0; loop < 5; loop++)
            {
                LogBarrierInformation("main task, start signaling and wait", barrier);
                barrier.SignalAndWait();
                LogBarrierInformation("main task waiting completed", barrier);
                //                var resultCollection = tasks[0].Result.Zip(tasks[1].Result, (c1, c2) => c1 + c2);
                int[][] resultCollection1 = taskResults[0];
                int[][] resultCollection2 = taskResults[1];
                var resultCollection = resultCollection1[loop].Zip(resultCollection2[loop], (c1, c2) => c1 + c2);

                char ch = 'a';
                int sum = 0;
                foreach (var x in resultCollection)
                {
                    WriteLine($"{ch++}, count: {x}");
                    sum += x;
                }

                LogBarrierInformation($"main task finished loop {loop}, sum: {sum}", barrier);
            }

            WriteLine("at the end");
            ReadLine();
        }
開發者ID:ProfessionalCSharp,項目名稱:ProfessionalCSharp6,代碼行數:53,代碼來源:Program.cs

示例3: DownloadGDBFiles

        private void DownloadGDBFiles()
        {
            finishFirstGDBSemaphore = new SemaphoreSlim(1);
            //finishFirstGDBSemaphore.Wait();

            Barrier downloadTrainingBarrier = new Barrier(1);
            
            foreach (int key in _currentTraining.Playlist.Keys)
            {
                Exercise exercise = _currentTraining.Playlist[key][1];
                int newKey = key;
               
                if (exercise.IsTraceable && !exercise.Downloaded)
                {
                    downloadTrainingBarrier.AddParticipant();

                    if (newKey == FIRST_EXERCISE)
                    {
                        finishFirstGDBSemaphore.Wait();
                    }
                    Task downloadGDB = new Task(() => DownloadCurrentGDB(exercise, newKey, downloadTrainingBarrier));
                    downloadGDB.Start(); 
                }

            }

            downloadTrainingBarrier.SignalAndWait();
            _currentTraining.Downloaded = true;

            //watch.Stop();
            //var elapsedMs = watch.ElapsedMilliseconds;
            //Console.WriteLine("==> Downloading gdb " + elapsedMs.ToString());
        }
開發者ID:amirben,項目名稱:VideoTherapy,代碼行數:33,代碼來源:TrainingMenu.xaml.cs

示例4: DownloadTreatment

        /// <summary>
        /// Download training preview images for the treatment screen
        /// </summary>
        public void DownloadTreatment()
        {
            //crearte dir for user (if not already created)
            //string newDir = CreateDir(dir, _patient.AccountId.ToString());
            //newDir = CreateDir(newDir, _patient.PatientTreatment.TreatmentNumber.ToString());
            string newDir = CreateDir(dir, "treatmentThumb");

            //initilaize the barrier
            int size = _patient.PatientTreatment.TrainingList.Count;
            //Barrier _barrier = new Barrier(size + 1);
            Barrier _barrier = new Barrier(1);

            for (int i = 0; i < size; i++)
            {
                int temp = i;

                string _from = _patient.PatientTreatment.TrainingList[temp].TrainingThumbs;
                string _to = newDir + "\\" + _patient.PatientTreatment.TrainingList[temp].TrainingId + ".png";
                _patient.PatientTreatment.TrainingList[temp].TrainingThumbs = _to;

                if (!File.Exists(_to))
                {
                    _barrier.AddParticipant();
                    Task tempThread = new Task(() =>
                    {
                        DownloadFile(_from, _to);
                        _barrier.SignalAndWait();
                    });
                    tempThread.Start();
                }

            }

            _barrier.SignalAndWait();

            _barrier.Dispose();
        }
開發者ID:amirben,項目名稱:VideoTherapy,代碼行數:40,代碼來源:DownloadCache.cs

示例5: RunSimulation

        /*
         * Running the simulation
         * */
        private void RunSimulation()
        {
            int sideSize = (int)Math.Ceiling((double)_simulationField.GetLength(0) / (double)MAX_SECTOR_SIZE);
            int threadsCount = sideSize * sideSize;

            _barrier = new Barrier(0);
            while (true)
            {

                if (_barrier.ParticipantCount > 0)
                {
                    _barrier.SignalAndWait();
                }

                for (int sector = 0; sector <= threadsCount; sector++)
                {
                    _barrier.AddParticipant();
                    ThreadPool.QueueUserWorkItem(new WaitCallback(new Action<object>(DoSector)),
                       sector);
                }

                Thread.Sleep(200);
            }
        }
開發者ID:mgechev,項目名稱:WaTor,代碼行數:27,代碼來源:Simulation.cs

示例6: SignalSimple

		public void SignalSimple ()
		{
			var barrier = new Barrier (0);
			barrier.AddParticipant ();
			Assert.IsTrue (barrier.SignalAndWait (500), "#1");
		}
開發者ID:JokerMisfits,項目名稱:linux-packaging-mono,代碼行數:6,代碼來源:BarrierTest.cs

示例7: RunBarrierTest7_Bug603035

        private static bool RunBarrierTest7_Bug603035()
        {
            TestHarness.TestLog("*RunBarrierTest7_Bug603035");
            bool failed = false;
            for (int j = 0; j < 100 && !failed; j++)
            {
                Barrier b = new Barrier(0);
               
                Action[] actions = Enumerable.Repeat((Action)(() =>
                {
                    for (int i = 0; i < 400; i++)
                    {
                        try
                        {
                            b.AddParticipant();
                            b.RemoveParticipant();
                        }
                        catch
                        {
                            failed = true;
                            break;
                        }
                    }

                }), 4).ToArray();
                Parallel.Invoke(actions);
                if (b.ParticipantCount != 0)
                    failed = true;
            }

            if (failed)
            {
                TestHarness.TestLog("Bug603035 failed");
                return false;
            }
            TestHarness.TestLog("Bug603035 succeeded");
            return true;

        }
開發者ID:modulexcite,項目名稱:IL2JS,代碼行數:39,代碼來源:BarrierTests.cs


注:本文中的System.Threading.Barrier.AddParticipant方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。