task-parallel-library

BlockingCollection with Parallel.For hangs?

房东的猫 提交于 2021-02-08 19:43:20
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in

BlockingCollection with Parallel.For hangs?

拜拜、爱过 提交于 2021-02-08 19:42:39
问题 I'm playing around with BlockingCollection to try to understand them better, but I'm struggling to understand why my code hangs when it finishes processing all my items when I use a Parallel.For I'm just adding a number to it (producer?): var blockingCollection = new BlockingCollection<long>(); Task.Factory.StartNew(() => { while (count <= 10000) { blockingCollection.Add(count); count++; } }); Then I'm trying to process (Consumer?): Parallel.For(0, 5, x => { foreach (long value in

Task parallel library INotifyPropertyChanged NOT throwing an exception?

孤人 提交于 2021-02-08 15:49:14
问题 I have a wpf project where I am using INotifyPropertyChanged on a property which binds to the textbox. I am updating this value on a different thread using task (TaskParallelLibrary). It is updated properly and does NOT throw an exception. I was thinking it would throw an exception because it is running on a background thread and not UI thread. Ofcourse it is throwing an exception if I directly use the UI element. So, does INotifyPropertyChanged bind mechanism takes care of dispatching to the

Should one use Task.Run within another Task?

别等时光非礼了梦想. 提交于 2021-02-08 07:55:52
问题 I have a bunch of work to do (either CPU-bound or IO-bound with no async interface to use) within a Task. I'm wondering if it's OK to just do all the non-async work within the task like this: async Task DoSomeStuff() { await SomethingAsync(); … DoCpuBoundWork(); … await SomethingElseAsync(); } or should I use Task.Run like this? async Task DoSomeStuff() { await SomethingAsync(); … await Task.Run(() => DoCpuBoundWork()); … await SomethingElseAsync(); } I know tasks aren't necessarily executed

Should one use Task.Run within another Task?

﹥>﹥吖頭↗ 提交于 2021-02-08 07:54:44
问题 I have a bunch of work to do (either CPU-bound or IO-bound with no async interface to use) within a Task. I'm wondering if it's OK to just do all the non-async work within the task like this: async Task DoSomeStuff() { await SomethingAsync(); … DoCpuBoundWork(); … await SomethingElseAsync(); } or should I use Task.Run like this? async Task DoSomeStuff() { await SomethingAsync(); … await Task.Run(() => DoCpuBoundWork()); … await SomethingElseAsync(); } I know tasks aren't necessarily executed

How ConfigureAwait(false) Prevent Ui Deadlocks

对着背影说爱祢 提交于 2021-02-08 06:31:12
问题 I Know that this question has been asked endlessly, but still. I think i am missing somthing. When we want updating UI( WinForm UI for the sake of argument ) in asynchronously manner. But we cannot using the async /await keyword and we have to use the ConfigureAwait(false). As i understand it "Tells" the task that it can resume itself on any available thread instead of waiting to to the main thread. Its preventing the deadlock by free the UI for not waiting for the long process to be

TPL Dataflow Transform block post to batch block followed by actionblock

泪湿孤枕 提交于 2021-02-07 20:27:57
问题 I have a TPL Dataflow based application, that worked fine using only a batch block, then an action block. I've added in a TransformBlock to try and trasnform the data from the source before posting to the batch block, but my action block is never getting hit. There are no errors or exceptions being thrown. I am unsure if I need to complete my transform block, as it only seems to be being hit once. Is there a step that I need to add to my transform code other than returning an object of the

TPL Dataflow Transform block post to batch block followed by actionblock

天大地大妈咪最大 提交于 2021-02-07 20:26:18
问题 I have a TPL Dataflow based application, that worked fine using only a batch block, then an action block. I've added in a TransformBlock to try and trasnform the data from the source before posting to the batch block, but my action block is never getting hit. There are no errors or exceptions being thrown. I am unsure if I need to complete my transform block, as it only seems to be being hit once. Is there a step that I need to add to my transform code other than returning an object of the

FileStream.ReadAsync very slow compared to Read()

本小妞迷上赌 提交于 2021-02-07 14:27:23
问题 I have the following code to loop thru a file and read 1024 bytes at a time. The first iteration uses FileStream.Read() and the second iteration uses FileStream.ReadAsync() . private async void Button_Click(object sender, RoutedEventArgs e) { await Task.Run(() => Test()).ConfigureAwait(false); } private async Task Test() { Stopwatch sw = new Stopwatch(); sw.Start(); int readSize; int blockSize = 1024; byte[] data = new byte[blockSize]; string theFile = @"C:\test.mp4"; long totalRead = 0;

FileStream.ReadAsync very slow compared to Read()

丶灬走出姿态 提交于 2021-02-07 14:22:10
问题 I have the following code to loop thru a file and read 1024 bytes at a time. The first iteration uses FileStream.Read() and the second iteration uses FileStream.ReadAsync() . private async void Button_Click(object sender, RoutedEventArgs e) { await Task.Run(() => Test()).ConfigureAwait(false); } private async Task Test() { Stopwatch sw = new Stopwatch(); sw.Start(); int readSize; int blockSize = 1024; byte[] data = new byte[blockSize]; string theFile = @"C:\test.mp4"; long totalRead = 0;