Tag: task parallel library

.NET 4相当于Task.WhenAll()

在.NET 4中,是否有任何function等同于.NET 4.5的System.Threading.Tasks.Task.WhenAll() ? 目标是将多个异步任务包装成一个在完成所有组成任务时完成的任务。

在ContinueWith中重新抛出先前的exception

介绍 在困惑我的代码一段时间后,我发现exception不一定通过ContinueWith传播: int zeroOrOne = 1; Task.Factory.StartNew(() => 3 / zeroOrOne) .ContinueWith(t => t.Result * 2) .ContinueWith(t => Console.WriteLine(t.Result)) .ContinueWith(_ => SetBusy(false)) .LogExceptions(); 在这个例子中, SetBusy行“重置”了exception链,因此看不到除零exception,随后在“我没有观察到任务exception……”的情况下爆炸。 所以……我给自己写了一个小扩展方法(有很多不同的重载,但基本上都是这样做的): public static Task ContinueWithEx(this Task task, Action continuation) { return task.ContinueWIth(t => { if(t.IsFaulted) throw t.Exception; continuation(t); }); } 搜索了一下,我发现了这篇博客文章,他在那里提出了类似的解决方案,但是使用了TaskCompletionSource,(转述)如下所示: public static Task ContinueWithEx(this Task task, Action continuation) { […]

使用Task.StartNew时指定线程的名称

有没有办法在使用Task.StartNew方法时指定Thread的名称 var task = Task.Factory.StartNew(MyAction, TaskCreationOption.LongRunning, ??ThreadName??);

TPL数据流加速?

我想知道以下代码是否可以优化以更快地执行。 在一个非常简单的数据流结构中,我目前似乎每秒大约有140万条简单消息。 我知道这个示例进程同步传递/转换消息,但是,我目前正在测试TPL Dataflow作为我自己的基于任务和并发集合的自定义解决方案的替代。 我知道术语“并发”已经建议我并行运行,但是出于当前的测试目的,我通过同步推送消息在我自己的解决方案上,每秒我得到大约510万条消息。 我在这里缺少什么,我读到TPL Dataflow被推为高吞吐量,低延迟的解决方案,但到目前为止我必须忽略性能调整。 有谁能指出我正确的方向吗? class TPLDataFlowExperiments { public TPLDataFlowExperiments() { var buf1 = new BufferBlock(); var transform = new TransformBlock(t => { return “”; }); var action = new ActionBlock(s => { //Thread.Sleep(100); //Console.WriteLine(s); }); buf1.LinkTo(transform); transform.LinkTo(action); //Propagate all Completions down the flow buf1.Completion.ContinueWith(t => { transform.Complete(); transform.Completion.ContinueWith(u => { action.Complete(); […]

c#像目标c一样调度队列

我想模仿c#中objective-c调度队列的行为。 我看到有一个任务并行库,但我真的不明白如何使用它,并希望得到一些解释如何。 在客观ci中会做类似的事情: -(void)doSomeLongRunningWorkAsync:(a_completion_handler_block)completion_handler { dispatch_async(my_queue, ^{ result *result_from_long_running_work = long_running_work(); completion_handler(result_from long_running_work); }); } -(void)aMethod { [self doSomeLongRunningWorkAsync:^(result *) { // the completion handler do_something_with_result_from_long_running_async_method_above; }]; } 这怎么被翻译成c#style任务并行库? 任何比较网站?

Task.Factory.StartNew vs Async方法

可能是一个微不足道的问题,但它可能有助于我的基本理解。 以下两个实现之间是否有任何重要区别? Task.Factory.StartNew : public Task ReadAllTextAsync(string path) { return Task.Factory.StartNew(() => File.ReadAllText(path)); } StreamReader上的异步方法: public async Task ReadAllTextAsync(string path) { using (var stream = File.OpenRead(path)) using (var reader = new StreamReader(stream)) { return await reader.ReadToEndAsync(); } }

Plinq语句在静态构造函数中陷入僵局

我遇到了这种情况,其中静态构造函数中的以下plinq语句变得死锁: static void Main(string[] args) { new Blah(); } class Blah { static Blah() { Enumerable.Range(1, 10000) .AsParallel() .Select(n => n * 3) .ToList(); } } 它只在构造函数是静态时才会发生。 请有人向我解释一下。 是TPL错误吗? 编译器? 我?

如何限制通过并行任务库运行的活动任务的数量?

我有一些包含Action(System.Action)的ConcurrentQueue。 需要运行此队列中的每个操作(需要使用invoke调用)。 当队列不为空时=>动作需要调用=>但是我想对将要运行的并行任务的数量进行一些限制。 除此之外,可以随时向队列添加新动作。 怎么做 ? (使用.net 4.0) 我写了一些东西,但我不确定这是最好的方法 SemaphoreSlim maxThread = new SemaphoreSlim(5); while( !actionQueue.IsEmpty ) { maxThread.Wait(); Task.Factory.StartNew( () => { Action action; if( actionExecution.TryDequeue( out action) ) { action.Invoke(); } }, TaskCreationOptions.LongRunning ).ContinueWith( ( task ) => maxThread.Release() ); } }

PagedList和Async

我在我的视图中使用了PagedList,但我的脚手架控制器是使用这种默认的索引操作生成的: public async Task Index() { return View(await db.Claimants.ToListAsync()); } 我没有找到PagedList的扩展名来使用async 。 我的方法必须改为这样的forms: public ActionResult Index(int? page) { var claimants = db.Claimants.OrderBy(b => b.Name); var notNullPage = page ?? 1; return View(claimants.ToPagedList(notNullPage, 50)); } 是否有合理的方式使用PagedList和async ?

ConcurrentBag 实现中是否存在内存泄漏?

可能重复: ConcurrentBag中可能的内存泄漏? EDIT1: 实际的问题是。 你能证实这一点,或者我的样本是错误的,我错过了一些明显的东西吗? 我认为ConcurrentBag是一个简单的替代无序列表。 但是我错了。 ConcurrentBag确实将自己作为ThreadLocal添加到创建线程,这基本上会导致内存泄漏。 class Program { static void Main(string[] args) { var start = GC.GetTotalMemory(true); new Program().Start(args); Console.WriteLine(“Diff: {0:N0} bytes”, GC.GetTotalMemory(true) – start); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Thread.Sleep(5000); } private void Start(string[] args) { for (int i = 0; i < 1000; i++) { var bag = new ConcurrentBag(); bag.Add(1); byte […]