相关疑难解决方法(0)

在Parallel.ForEach中嵌套等待

在metro应用程序中,我需要执行许多WCF调用.有大量的调用,所以我需要在并行循环中进行调用.问题是并行循环在WCF调用完成之前退出.

你会如何重构这个按预期工作?

var ids = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
var customers = new  System.Collections.Concurrent.BlockingCollection<Customer>();

Parallel.ForEach(ids, async i =>
{
    ICustomerRepo repo = new CustomerRepo();
    var cust = await repo.GetCustomer(i);
    customers.Add(cust);
});

foreach ( var customer in customers )
{
    Console.WriteLine(customer.ID);
}

Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)

c# wcf task-parallel-library async-await parallel.foreach

159
推荐指数
9
解决办法
10万
查看次数

如何限制并发异步I/O操作的数量?

// let's say there is a list of 1000+ URLs
string[] urls = { "http://google.com", "http://yahoo.com", ... };

// now let's send HTTP requests to each of these URLs in parallel
urls.AsParallel().ForAll(async (url) => {
    var client = new HttpClient();
    var html = await client.GetStringAsync(url);
});
Run Code Online (Sandbox Code Playgroud)

这是问题所在,它会同时启动1000多个Web请求.有没有一种简单的方法来限制这些异步http请求的并发数量?这样在任何给定时间都不会下载超过20个网页.如何以最有效的方式做到这一点?

c# asynchronous task-parallel-library async-await async-ctp

103
推荐指数
5
解决办法
4万
查看次数

与异步lambda并行的foreach

我想并行处理一个集合,但是我在实现它时遇到了麻烦,因此我希望得到一些帮助.

如果我想在并行循环的lambda中调用C#中标记为async的方法,则会出现问题.例如:

var bag = new ConcurrentBag<object>();
Parallel.ForEach(myCollection, async item =>
{
  // some pre stuff
  var response = await GetData(item);
  bag.Add(response);
  // some post stuff
}
var count = bag.Count;
Run Code Online (Sandbox Code Playgroud)

计数为0时会出现问题,因为创建的所有线程实际上只是后台线程,并且Parallel.ForEach调用不等待完成.如果我删除async关键字,该方法如下所示:

var bag = new ConcurrentBag<object>();
Parallel.ForEach(myCollection, item =>
{
  // some pre stuff
  var responseTask = await GetData(item);
  responseTask.Wait();
  var response = responseTask.Result;
  bag.Add(response);
  // some post stuff
}
var count = bag.Count;
Run Code Online (Sandbox Code Playgroud)

它工作,但它完全禁用等待聪明,我必须做一些手动异常处理..(为简洁起见删除).

如何实现一个Parallel.ForEach在lambda中使用await关键字的循环?可能吗?

Parallel.ForEach方法的原型采用Action<T>as参数,但我希望它等待我的异步lambda.

c# task-parallel-library async-await parallel.foreach

100
推荐指数
6
解决办法
7万
查看次数

具有支持多线程的限制的异步任务的队列

我需要实现一个库来请求vk.com API.问题是API每秒只支持3个请求.我想让API异步.

重要: API应支持从多个线程安全访问.

我的想法是实现一个名为throttler的类,它允许不超过3个请求/秒并延迟其他请求.

接口是下一个:

public interface IThrottler : IDisposable
{
    Task<TResult> Throttle<TResult>(Func<Task<TResult>> task);
}
Run Code Online (Sandbox Code Playgroud)

用法就像

var audio = await throttler.Throttle(() => api.MyAudio());
var messages = await throttler.Throttle(() => api.ReadMessages());
var audioLyrics = await throttler.Throttle(() => api.AudioLyrics(audioId));
/// Here should be delay because 3 requests executed
var photo = await throttler.Throttle(() => api.MyPhoto());
Run Code Online (Sandbox Code Playgroud)

如何实施throttler?

目前我将其实现为由后台线程处理的队列.

public Task<TResult> Throttle<TResult>(Func<Task<TResult>> task)
{
    /// TaskRequest has method Run() to run task
    /// TaskRequest uses TaskCompletionSource to provide new task 
    /// which is …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading async-await

9
推荐指数
1
解决办法
3915
查看次数

在一些.NET Parallel.ForEach()代码中做一些异步/等待是否可以?

考虑下面的代码,是不是OKasync/await里面Parallel.ForEach

例如.

Parallel.ForEach(names, name =>
{
    // Do some stuff...

    var foo = await GetStuffFrom3rdPartyAsync(name);

    // Do some more stuff, with the foo.
});
Run Code Online (Sandbox Code Playgroud)

还是有一些我需要知道的问题?

编辑:不知道这是否编译,顺便说一句.只是Pseduo代码..大声思考.

.net c# parallel-processing async-await parallel.foreach

7
推荐指数
3
解决办法
4054
查看次数