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


C# Task.OfType方法代码示例

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


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

示例1: Should_Just_Work

		public async Task Should_Just_Work()
		{
			/* Setup */
			using (var requester = TestClientFactory.CreateNormal())
			using (var responder = TestClientFactory.CreateNormal())
			{
				const int numberOfCalls = 10000;
				var array = new Task[numberOfCalls];
				responder.RespondAsync<FirstRequest, FirstResponse>((req, i) =>
					Task.FromResult(new FirstResponse { Infered = Guid.NewGuid() })
				);

				/* Test */
				var sw = new Stopwatch();
				sw.Start();
				for (var i = 0; i < numberOfCalls; i++)
				{
					var response = requester.RequestAsync<FirstRequest, FirstResponse>();
					array[i] = response;
				}
				Task.WaitAll(array);
				sw.Stop();
				var ids = array
					.OfType<Task<FirstResponse>>()
					.Select(b => b.Result.Infered)
					.Where(id => id != Guid.Empty)
					.Distinct()
					.ToList();

				/* Assert */
				Assert.Equal(expected: numberOfCalls, actual: ids.Count);
			}
		}
开发者ID:pardahlman,项目名称:RawRabbit,代码行数:33,代码来源:MultipleRequestsTests.cs

示例2: Should_Work_For_Multiple_Types

		public async Task Should_Work_For_Multiple_Types()
		{
			/* Setup */
			using (var requester = TestClientFactory.CreateNormal())
			using (var responder = TestClientFactory.CreateNormal())
			{
				const int numberOfCalls = 10000;
				var firstResponseTasks = new Task[numberOfCalls];
				var secondResponseTasks = new Task[numberOfCalls];
				responder.RespondAsync<FirstRequest, FirstResponse>((req, i) =>
					Task.FromResult(new FirstResponse { Infered = Guid.NewGuid() }));
				responder.RespondAsync<SecondRequest, SecondResponse>((request, context) =>
					Task.FromResult(new SecondResponse { Source = Guid.NewGuid() }));

				/* Test */
				for (var i = 0; i < numberOfCalls; i++)
				{
					var firstResponse = requester.RequestAsync<FirstRequest, FirstResponse>();
					var secondResponse = requester.RequestAsync<SecondRequest, SecondResponse>();
					firstResponseTasks[i] = firstResponse;
					secondResponseTasks[i] = secondResponse;
				}
				Task.WaitAll(firstResponseTasks.Concat(secondResponseTasks).ToArray());
				var firstIds = firstResponseTasks
					.OfType<Task<FirstResponse>>()
					.Select(b => b.Result.Infered)
					.Where(id => id != Guid.Empty)
					.Distinct()
					.ToList();
				var secondIds = secondResponseTasks
					.OfType<Task<SecondResponse>>()
					.Select(b => b.Result.Source)
					.Where(id => id != Guid.Empty)
					.Distinct()
					.ToList();
				/* Assert */
				Assert.Equal(expected: numberOfCalls, actual: firstIds.Count);
				Assert.Equal(expected: numberOfCalls, actual: secondIds.Count);
			}
		}
开发者ID:pardahlman,项目名称:RawRabbit,代码行数:40,代码来源:MultipleRequestsTests.cs

示例3: Should_Work_When_Not_Awaiting_One_Response_At_A_Time

		public async Task Should_Work_When_Not_Awaiting_One_Response_At_A_Time()
		{
			/* Setup */
			const int numberOfCalls = 10;
			var tasks = new Task[numberOfCalls];
			var requester = BusClientFactory.CreateDefault();
			var responder = BusClientFactory.CreateDefault();

			responder.RespondAsync<FirstRequest, FirstResponse>((req, i) => Task.FromResult(new FirstResponse { Infered = Guid.NewGuid()}));

			/* Test */
			for (int i = 0; i < numberOfCalls; i++)
			{
				var responseTask = requester.RequestAsync<FirstRequest, FirstResponse>();
				tasks[i] = responseTask;
			}
			Task.WaitAll(tasks);
			var ids = tasks
				.OfType<Task<FirstResponse>>()
				.Select(t => t.Result.Infered)
				.Distinct()
				.ToList();

			/* Assert */
			Assert.Equal(ids.Count, numberOfCalls);
		}
开发者ID:futzy314,项目名称:RawRabbit,代码行数:26,代码来源:RpcTest.cs

示例4: GetMedicines

        private List<MedicineRaw> GetMedicines()
        {
            string page1 = this.GetResponseTextWithCache(1);
            var document1 = new HtmlDocument();
            document1.LoadHtml(page1);
            HtmlNode linkFim = document1.DocumentNode.SelectSingleNode("//a[contains(text(), 'Fim')]");
            string hrefLinkFim = linkFim.GetAttributeValue("href", "");
            int regexCountPages = int.Parse(Regex.Match(hrefLinkFim, @"(?<=&pagina=)\d+").Value);

            var listTasks = new Task<List<MedicineRaw>>[regexCountPages];
            for (int pagina = 0; pagina < regexCountPages; pagina++)
                listTasks[pagina] = new Task<List<MedicineRaw>>(this.GetItems, pagina + 1);

            foreach (var eachTask in listTasks)
                eachTask.Start();

            Task.WaitAll(listTasks.OfType<Task>().ToArray());

            List<MedicineRaw> medicines = listTasks.SelectMany(t => t.Result).ToList();

            return medicines;
        }
开发者ID:andrerpena,项目名称:Cerebello,代码行数:22,代码来源:AnvisaLeafletHelper.cs

示例5: Should_Work_When_Not_Awaiting_One_Response_At_A_Time

		public async Task Should_Work_When_Not_Awaiting_One_Response_At_A_Time()
		{
			/* Setup */
			using (var requester = TestClientFactory.CreateNormal())
			using (var responder = TestClientFactory.CreateNormal())
			{
				const int numberOfCalls = 10;
				var tasks = new Task[numberOfCalls];

				responder.RespondAsync<FirstRequest, FirstResponse>((req, i) =>
					Task.FromResult(new FirstResponse { Infered = Guid.NewGuid() }),
					cfg => cfg.WithQueue(q => q.WithAutoDelete()));

				/* Test */
				for (int i = 0; i < numberOfCalls; i++)
				{
					var responseTask = requester.RequestAsync<FirstRequest, FirstResponse>();
					tasks[i] = responseTask;
				}
				Task.WaitAll(tasks);
				var ids = tasks
					.OfType<Task<FirstResponse>>()
					.Select(t => t.Result.Infered)
					.Distinct()
					.ToList();

				/* Assert */
				Assert.Equal(expected: numberOfCalls, actual: ids.Count);
			}
		}
开发者ID:pardahlman,项目名称:RawRabbit,代码行数:30,代码来源:RpcTest.cs


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