本文整理汇总了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.");
}
}
示例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
}
}
示例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;
}