当前位置: 首页>>代码示例>>C#>>正文


C# ConcurrentStack.TryPopRange方法代码示例

本文整理汇总了C#中ConcurrentStack.TryPopRange方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.TryPopRange方法的具体用法?C# ConcurrentStack.TryPopRange怎么用?C# ConcurrentStack.TryPopRange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ConcurrentStack的用法示例。


在下文中一共展示了ConcurrentStack.TryPopRange方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            //Stack - Pilha (LIFO) 
            ConcurrentStack<int> stack = new ConcurrentStack<int>();
            //Adiciona um item na pilha
            stack.Push(42);

            int result;
     
            //metodo trypop retorna o ultimo item a ser adicionado na lista, caso não tenha mais item ele não dar por que ele "tenta(try)" pega um item
            //quando usar o metodo trypop o item é removido da coleção
            if (stack.TryPop(out result))
            {
                Console.WriteLine("Popped: {0}", result);
            }
            if (stack.TryPop(out result))
            {
                Console.WriteLine("Popped: {0}", result);
            }

            stack.PushRange(new int[] { 1, 2, 3 });

            int[] values = new int[2];
            //metod retorna uma coleção de itens da pilha
            stack.TryPopRange(values);

            foreach (var item in values)
            {
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }
开发者ID:Willamar,项目名称:ExamRef-70-483,代码行数:33,代码来源:Program.cs

示例2: Main

        static void Main(string[] args)
        {
            ConcurrentStack<int> stack = new ConcurrentStack<int>();

            stack.Push(42);

            int result;
            if (stack.TryPop(out result))
            {
                Console.WriteLine(result);
            }

            stack.PushRange(new int[] { 1, 2, 3 });

            int[] values = new int[2];
            stack.TryPopRange(values);

            foreach (var i in values)
            {
                Console.WriteLine(i);
            }

            Console.Write("Press a key to exit");
            Console.ReadKey();
        }
开发者ID:jbijoux,项目名称:Exam70_483,代码行数:25,代码来源:Program.cs

示例3: ConcurrentStackExampleRange

		public int ConcurrentStackExampleRange ()
		{
			var stack = new ConcurrentStack<int> (); 

			stack.PushRange (new int[] { 1, 2, 3 }); 
		
			return stack.TryPopRange (new int[2]); 
		}
开发者ID:caloggins,项目名称:DOT-NET-on-Linux,代码行数:8,代码来源:ConcurrentStackExample.cs

示例4: Test0_Empty

        public static void Test0_Empty()
        {
            ConcurrentStack<int> s = new ConcurrentStack<int>();
            int item;
            Assert.False(s.TryPop(out item), "Test0_Empty:  TryPop returned true when the stack is empty");
            Assert.False(s.TryPeek(out item), "Test0_Empty:  TryPeek returned true when the stack is empty");
            Assert.True(s.TryPopRange(new int[1]) == 0, "Test0_Empty:  TryPopRange returned non zero when the stack is empty");

            int count = 15;
            for (int i = 0; i < count; i++)
                s.Push(i);

            Assert.Equal(count, s.Count);
            Assert.False(s.IsEmpty);
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:15,代码来源:ConcurrentStackTests.cs

示例5: Run

        public void Run()
        {
            ConcurrentStack<int> stack = new ConcurrentStack<int>();
            stack.Push(42);

            int result;
            if (stack.TryPop(out result))
                Console.WriteLine("Popped: {0}", result);

            stack.PushRange(new int[] { 1, 2, 3 });
            int[] values = new int[2];
            stack.TryPopRange(values);

            foreach (int i in values)
                Console.WriteLine(i);
        }
开发者ID:vikramadhav,项目名称:Certification_70-483,代码行数:16,代码来源:Listing_1_32.cs

示例6: RunConcurrentStack

        private void RunConcurrentStack()
        {
            Console.WriteLine("Stack Run implementation along with Range implementations");
            ConcurrentStack<int> stack = new ConcurrentStack<int>();
            stack.Push(10);

            int result;
            if (stack.TryPop(out result))
                Console.WriteLine(result);

            stack.PushRange(new int[] { 1, 2, 3 ,4});

            int[] values = new int[4];
            Console.WriteLine(stack.TryPopRange(values));

            foreach (int num in values) Console.WriteLine(num);
        }
开发者ID:mayankaggarwal,项目名称:MyConcepts,代码行数:17,代码来源:Concept19.cs

示例7: Main

        private static void Main()
        {
            var stack = new ConcurrentStack<int>();
            stack.Push(42);

            int result;
            if (stack.TryPop(out result)) {
                Console.WriteLine(result);
            }

            stack.PushRange(new[] {1, 2, 3});
            var values = new int[2];
            stack.TryPopRange(values);

            foreach (var i in values) {
                Console.WriteLine(i);
            }

            Console.ReadLine();
        }
开发者ID:DriesPeeters,项目名称:examprep-70_483,代码行数:20,代码来源:Program.cs

示例8: Main

        static void Main(string[] args)
        {
            ConcurrentStack<int> stack = new ConcurrentStack<int>();
            stack.Push(67);

            int result;
            if (stack.TryPop(out result))
                Console.WriteLine("Popped: {0}", result);
            stack.PushRange(new int[] { 1, 2, 3 });
            int[] values = new int[2];
            stack.TryPopRange(values);
            foreach (int i in values)
                Console.WriteLine(i);

            Console.WriteLine("\n\n----------------------------------------------------------------------\n\n");
            Console.ReadLine();

            var dict = new ConcurrentDictionary<string, int>();
            if (dict.TryAdd("ERHAN",26))
            {
                Console.WriteLine("Added");
            }
            if (dict.TryUpdate("ERHAN", 30, 26))
            {
                Console.WriteLine("26 updated to 30");
            }

            dict["ERHAN"] = 32; // Overwrite unconditionally

            Console.WriteLine("----------------------------------------------------------------------");
            Console.ReadLine();

            int r = dict.AddOrUpdate("ERHAN", 35, (S, I) => I * 2);
            Console.WriteLine(r);
            int r2 = dict.GetOrAdd("ERHAN", 37);

            Console.WriteLine("----------------------------------------------------------------------");
            Console.ReadLine();
        }
开发者ID:ErhanGDC,项目名称:MyWorks,代码行数:39,代码来源:Program.cs

示例9: Main

        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            ConcurrentStack<int> stack = new ConcurrentStack<int>();
            stack.Push(42);

            int result;
            if (stack.TryPop(out result))
                Console.WriteLine("Popped: {0}", result);

            stack.PushRange(new int[] { 1, 2, 3 });

            int[] values = new int[2];
            stack.TryPopRange(values);

            foreach (int i in values)
                Console.WriteLine(i);

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
开发者ID:vmp,项目名称:CSharpExamples,代码行数:22,代码来源:Program.cs

示例10: Main

        public static void Main(string[] args)
        {
            ConcurrentStack<int> stack = new ConcurrentStack<int>();

            stack.Push(42);

            int result;
            if (stack.TryPop(out result))
            {
                Console.WriteLine("Popped: {0}", result);
            }

            stack.PushRange(new int[] { 1, 2, 3 });

            int[] values = new int[2];
            stack.TryPopRange(values);

            foreach (var i in values)
            {
                Console.WriteLine(i);
            }

            Console.ReadLine();
        }
开发者ID:nissbran,项目名称:Training-Certifications,代码行数:24,代码来源:Program.cs

示例11: TryPopRangeEmpty

		public void TryPopRangeEmpty ()
		{
			stack = new ConcurrentStack<int>();
			Assert.AreEqual (0, stack.TryPopRange (new int [1]));
		}
开发者ID:Profit0004,项目名称:mono,代码行数:5,代码来源:ConcurrentStackTests.cs

示例12: RunConcurrentStackTest7_PopRange

        //Tests ConcurrentStack.PopRange by pushing consecutove numbers and run n threads each thread tries to pop m itmes
        // the popped m items should be consecutive
        private static bool RunConcurrentStackTest7_PopRange(int NumOfThreads, int elementsPerThread)
        {
            TestHarness.TestLog("* RunConcurrentStackTest7_PopRange({0},{1})", NumOfThreads, elementsPerThread);
            ConcurrentStack<int> stack = new ConcurrentStack<int>(Enumerable.Range(1, NumOfThreads * elementsPerThread));


            Thread[] threads = new Thread[NumOfThreads];

            int[] array = new int[threads.Length * elementsPerThread];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread((obj) =>
                {
                    int index = (int)obj;

                    int res;
                    if ((res = stack.TryPopRange(array, index, elementsPerThread)) != elementsPerThread)
                    {
                        TestHarness.TestLog(" > Failed TryPopRange didn't return the full range ");
                    }

                });

                threads[i].Start(i * elementsPerThread);

            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            // validation
            for (int i = 0; i < NumOfThreads; i++)
            {
                for (int j = 1; j < elementsPerThread; j++)
                {
                    int currentIndex = i * elementsPerThread + j;
                    if (array[currentIndex - 1] - array[currentIndex] != 1)
                    {
                        TestHarness.TestLog(" > Failed {0} - {1} shouldn't be consecutive", array[currentIndex - 1], array[currentIndex]);
                        return false;
                    }
                }
            }

            return true;

        }
开发者ID:modulexcite,项目名称:IL2JS,代码行数:51,代码来源:CdsTests.cs

示例13: Test8_Exceptions

        public static void Test8_Exceptions()
        {
            ConcurrentStack<int> stack = null;
            Assert.Throws<ArgumentNullException>(
               () => stack = new ConcurrentStack<int>((IEnumerable<int>)null));
               // "Test8_Exceptions:  The constructor didn't throw ANE when null collection passed");

            stack = new ConcurrentStack<int>();
            //CopyTo
            Assert.Throws<ArgumentNullException>(
               () => stack.CopyTo(null, 0));
               // "Test8_Exceptions:  CopyTo didn't throw ANE when null array passed");
            Assert.Throws<ArgumentOutOfRangeException>(
               () => stack.CopyTo(new int[1], -1));
               // "Test8_Exceptions:  CopyTo didn't throw AORE when negative array index passed");

            //PushRange
            Assert.Throws<ArgumentNullException>(
               () => stack.PushRange(null));
               // "Test8_Exceptions:  PushRange didn't throw ANE when null array passed");
            Assert.Throws<ArgumentOutOfRangeException>(
               () => stack.PushRange(new int[1], 0, -1));
               // "Test8_Exceptions:  PushRange didn't throw AORE when negative count passed");
            Assert.Throws<ArgumentOutOfRangeException>(
               () => stack.PushRange(new int[1], -1, 1));
               // "Test8_Exceptions:  PushRange didn't throw AORE when negative index passed");
            Assert.Throws<ArgumentOutOfRangeException>(
               () => stack.PushRange(new int[1], 2, 1));
               // "Test8_Exceptions:  PushRange didn't throw AORE when start index > array length");
            Assert.Throws<ArgumentException>(
               () => stack.PushRange(new int[1], 0, 10));
               // "Test8_Exceptions:  PushRange didn't throw AE when count + index > array length");

            //PopRange
            Assert.Throws<ArgumentNullException>(
               () => stack.TryPopRange(null));
               // "Test8_Exceptions:  TryPopRange didn't throw ANE when null array passed");
            Assert.Throws<ArgumentOutOfRangeException>(
               () => stack.TryPopRange(new int[1], 0, -1));
               // "Test8_Exceptions:  TryPopRange didn't throw AORE when negative count passed");
            Assert.Throws<ArgumentOutOfRangeException>(
               () => stack.TryPopRange(new int[1], -1, 1));
               // "Test8_Exceptions:  TryPopRange didn't throw AORE when negative index passed");
            Assert.Throws<ArgumentOutOfRangeException>(
               () => stack.TryPopRange(new int[1], 2, 1));
               // "Test8_Exceptions:  TryPopRange didn't throw AORE when start index > array length");
            Assert.Throws<ArgumentException>(
               () => stack.TryPopRange(new int[1], 0, 10));
               // "Test8_Exceptions:  TryPopRange didn't throw AE when count + index > array length");
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:50,代码来源:ConcurrentStackTests.cs

示例14: Test7_PopRange

        //Tests ConcurrentStack.PopRange by pushing consecutive numbers and run n threads each thread tries to pop m itmes
        // the popped m items should be consecutive
        private static void Test7_PopRange(int NumOfThreads, int elementsPerThread)
        {
            int lastValue = NumOfThreads * elementsPerThread;
            List<int> allValues = new List<int>();
            for (int i = 1; i <= lastValue; i++)
                allValues.Add(i);

            ConcurrentStack<int> stack = new ConcurrentStack<int>(allValues);

            Task[] threads = new Task[NumOfThreads];

            int[] array = new int[threads.Length * elementsPerThread];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = Task.Factory.StartNew((obj) =>
                {
                    int index = (int)obj;

                    int res = stack.TryPopRange(array, index, elementsPerThread);
                    Assert.Equal(elementsPerThread, res);
                }, i * elementsPerThread, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
            }

            Task.WaitAll(threads);

            // validation
            for (int i = 0; i < NumOfThreads; i++)
            {
                for (int j = 1; j < elementsPerThread; j++)
                {
                    int currentIndex = i * elementsPerThread + j;
                    Assert.Equal(array[currentIndex - 1], array[currentIndex] + 1);
                }
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:37,代码来源:ConcurrentStackTests.cs


注:本文中的ConcurrentStack.TryPopRange方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。