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


C# Task.Any方法代码示例

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


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

示例1: UpsertBlockOrSkipWithStressAsync

        public void UpsertBlockOrSkipWithStressAsync()
        {
            BlobStorage.PutBlobAsync(ContainerName, BlobName, 0).Wait();

            var array = new Task<BlobWithETag<int>>[8];
            array = array
                .AsParallel()
                .Select(k => BlobStorage.UpsertBlobOrSkipAsync<int>(ContainerName, BlobName, () => 1, i => i + 1))
                .ToArray();

            Assert.IsFalse(array.Any(x => x.Result == null), "No skips");

            var sorted = array.Select(m => m.Result.Blob)
                .OrderBy(i => i)
                .ToArray();

            for (int i = 0; i < array.Length; i++)
            {
                Assert.AreEqual(i + 1, sorted[i], "Concurrency should be resolved, every call should increment by one.");
            }
        }
开发者ID:pavelhritonenko,项目名称:lokad-cloud-storage,代码行数:21,代码来源:BlobStorageTests.cs

示例2: Main

        static void Main(string[] args)
        {
            var maxPlayers = 6;
            var rand = new Random();

            while (true)
            {

                var numOfPlayers = rand.Next(maxPlayers / 2, maxPlayers);
                Task[] tasks = new Task[numOfPlayers];
                for (int j = 0; j < numOfPlayers; j++)
                {
                    int j1 = j;

                    tasks[j] = Task.Factory.StartNew(() => Target(new ThreadParams()
                                                                      {
                                                                          MaxUsers=numOfPlayers,
                                                                          Seed = j1 * 6,
                                                                          State = (j1 == 0 ? (ThreadState.CreateGame) : ThreadState.JoinGame),
                                                                          UserName = names[j1]
                                                                      }));
                }
                while (tasks.Any(t => !t.IsCompleted)) { } //spin wait

            }
        }
开发者ID:Shuffle-Game,项目名称:ShuffleSlam,代码行数:26,代码来源:Program.cs

示例3: ExecuteBenchmarks

        /// <summary>
        /// Executes the benchmarks.
        /// </summary>
        /// <param name="numOfThreads">The num of threads.</param>
        /// <param name="benchmarkType">Type of the benchmark.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns></returns>
        private static BenchmarkBase[] ExecuteBenchmarks(int numOfThreads, Type benchmarkType, params object[] constructorParameters) {
            BenchmarkBase[] benchmarks = new BenchmarkBase[numOfThreads];

            //Create threads
            Mutex mutex = new Mutex(true, Settings.MUTEX_NAME);

            #if THREADS
                Thread[] tasks = new Thread[numOfThreads];
            #else
                Task[] tasks = new Task[numOfThreads];
            #endif

            for (int i = 0; i < numOfThreads; i++) {
                benchmarks[i] = Activator.CreateInstance(benchmarkType, constructorParameters) as BenchmarkBase;

                #if THREADS
                    tasks[i] = new Thread(new ThreadStart(benchmarks[i].ExecuteBenchmark));
                #else
                    tasks[i] = new Task(new Action(benchmarks[i].ExecuteBenchmark));
                #endif
                tasks[i].Start();
            }

            //Start nechmark execution
            mutex.ReleaseMutex();
            mutex.Dispose();

            //Wait for threads to complete execution
            #if THREADS
                while (tasks.Any(t => t.IsAlive)) { }
            #else
                while (tasks.Any(t => !t.IsCompleted)) { }
            #endif

            return benchmarks;
        }
开发者ID:gaurangsinha,项目名称:MongoBench,代码行数:43,代码来源:BenchmarkServer.cs


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