task-parallel-library

TPL Dataflow, whats the functional difference between Post() and SendAsync()?

大城市里の小女人 提交于 2019-12-28 05:35:09
问题 I am confused about the difference between sending items through Post() or SendAsync(). My understanding is that in all cases once an item reached the input buffer of a data block, control is returned to the calling context, correct? Then why would I ever need SendAsync? If my assumption is incorrect then I wonder, on the contrary, why anyone would ever use Post() if the whole idea of using data blocks is to establish a concurrent and async environment. I understand of course the difference

Send multiple WebRequest in Parallel.For

旧街凉风 提交于 2019-12-28 04:24:06
问题 I want to send multiple WebRequest . I used a Parallel.For loop to do that but the loop runs once and the second time it gives error while getting response. Error: The operation has timed out Code : Parallel.For(0, 10, delegate(int i) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create( new Uri("http://www.mysite.com/service")); string dataToSend = "Data"; byte[] buffer = System.Text.Encoding.GetEncoding(1252). GetBytes(dataToSend); request.Method = "POST"; request.ContentType =

How can I capture the value of an outer variable inside a lambda expression?

江枫思渺然 提交于 2019-12-28 04:07:48
问题 I just encountered the following behavior: for (var i = 0; i < 50; ++i) { Task.Factory.StartNew(() => { Debug.Print("Error: " + i.ToString()); }); } Will result in a series of "Error: x", where most of the x are equal to 50. Similarly: var a = "Before"; var task = new Task(() => Debug.Print("Using value: " + a)); a = "After"; task.Start(); Will result in "Using value: After". This clearly means that the concatenation in the lambda expression does not occur immediately. How is it possible to

can not await async lambda

懵懂的女人 提交于 2019-12-28 03:32:04
问题 Consider this, Task task = new Task (async () =>{ await TaskEx.Delay(1000); }); task.Start(); task.Wait(); The call task.Wait() does not wait for the task completion and the next line is executed immediately, but if I wrap the async lambda expression into a method call, the code works as expected. private static async Task AwaitableMethod() { await TaskEx.Delay(1000); } then (updated according comment from svick) await AwaitableMethod(); 回答1: In your lambda example, when you call task.Wait()

Why is TaskScheduler.Current the default TaskScheduler?

隐身守侯 提交于 2019-12-28 02:26:29
问题 The Task Parallel Library is great and I've used it a lot in the past months. However, there's something really bothering me: the fact that TaskScheduler.Current is the default task scheduler, not TaskScheduler.Default. This is absolutely not obvious at first glance in the documentation nor samples. Current can lead to subtle bugs since its behavior is changing depending on whether you're inside another task. Which can't be determined easily. Suppose I am writting a library of asynchronous

Use Task.Run() in synchronous method to avoid deadlock waiting on async method?

ぃ、小莉子 提交于 2019-12-27 19:10:10
问题 UPDATE The purpose of this question is to get a simple answer about Task.Run() and deadlocking. I very much understand the theoretical reasoning for not mixing async and sync, and I take them to heart. I'm not above learning new things from others; I seek to do that whenever I can. There's just times when all a guy needs is a technical answer... I have a Dispose() method that needs to call an async method. Since 95% of my code is async, refactoring isn't the best choice. Having an

Is it considered acceptable to not call Dispose() on a TPL Task object?

匆匆过客 提交于 2019-12-27 10:43:07
问题 I want to trigger a task to run on a background thread. I don't want to wait on the tasks completion. In .net 3.5 I would have done this: ThreadPool.QueueUserWorkItem(d => { DoSomething(); }); In .net 4 the TPL is the suggested way. The common pattern I have seen recommended is: Task.Factory.StartNew(() => { DoSomething(); }); However, the StartNew() method returns a Task object which implements IDisposable . This seems to be overlooked by people who recommend this pattern. The MSDN

Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

淺唱寂寞╮ 提交于 2019-12-27 10:21:53
问题 I just saw 3 routines regarding TPL usage which do the same job; here is the code: public static void Main() { Thread.CurrentThread.Name = "Main"; // Create a task and supply a user delegate by using a lambda expression. Task taskA = new Task( () => Console.WriteLine("Hello from taskA.")); // Start the task. taskA.Start(); // Output a message from the calling thread. Console.WriteLine("Hello from thread '{0}'.", Thread.CurrentThread.Name); taskA.Wait(); } public static void Main() { Thread

Regarding usage of Task.Start() , Task.Run() and Task.Factory.StartNew()

末鹿安然 提交于 2019-12-27 10:20:54
问题 I just saw 3 routines regarding TPL usage which do the same job; here is the code: public static void Main() { Thread.CurrentThread.Name = "Main"; // Create a task and supply a user delegate by using a lambda expression. Task taskA = new Task( () => Console.WriteLine("Hello from taskA.")); // Start the task. taskA.Start(); // Output a message from the calling thread. Console.WriteLine("Hello from thread '{0}'.", Thread.CurrentThread.Name); taskA.Wait(); } public static void Main() { Thread

TAP global exception handler

久未见 提交于 2019-12-27 09:48:22
问题 This code throws an exception. Is it possible to define an application global handler that will catch it? string x = await DoSomethingAsync(); Using .net 4.5 / WPF 回答1: This is actually a good question, if I understood it correctly. I initially voted to close it, but now retracted my vote. It is important to understand how an exception thrown inside an async Task method gets propagated outside it. The most important thing is that such exception needs to be observed by the code which handles