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