task-parallel-library

Async CTP Bug - Task Never Completes

微笑、不失礼 提交于 2020-01-02 05:38:09
问题 Firstly a forward apology: I cannot isolate the following bug into a simple console application. However, in my relatively simple ASP.NET Web Forms application, the following code will cause the current thread to block indefinitely: public class MyModule : IHttpModule { public void Dispose() { } public void Init(System.Web.HttpApplication context) { context.BeginRequest += this.Context_BeginRequest; } private void Context_BeginRequest(object sender, EventArgs e) { Sleep().Wait(); var x = 2; /

TPL How to Perform a 'Call-Back'

巧了我就是萌 提交于 2020-01-02 04:40:31
问题 I have a small application that needs to test SQL connection strings for a number of connections (each done one-at-a-time). To do this I set the ConnectionTimeout = 5 temporarily to avoid a long wait if the connection is not valid and ConnectionTimeout = 0 (wait forever), say. To avoid the UI hanging whilst we attempt to Open() a bad connection (even with ConnectionTimeout = 5 the wait for the SqlException can be up to twenty seconds), I want to run the test on a separate thread using Task

Async code without waiting for completion

烂漫一生 提交于 2020-01-02 03:27:07
问题 I'm currently having issues with IIS crashing out leaving the following messages in the event log. They're not too helpful in directing me to the actual source of the error but a bit of research suggests this is just a case of spawning tasks but not waiting for the result, when they do eventually complete if the parent process has completed it cannot be associated with the parent thread which causes a null reference exception. Is that correct? For the most part I have removed or added awaits

Create but not start a task with a custom task factory?

梦想与她 提交于 2020-01-02 02:59:12
问题 I'd like to be able to create a task without starting it, similar to running var a = new Task(); a.Start(); but with a custom factory. Factories provide StartNew() , but I can't find a method to separate the two actions. Is this possible? 回答1: A TaskFactory is basically two sets of default options (creation and continuation), a default cancellation token, and a task scheduler. You can specify the cancellation token and the creation options when you just new up a task - and then start it on

How to consume HttpClient from F#?

匆匆过客 提交于 2020-01-02 01:05:10
问题 I'm new to F# and stuck in understanding async in F# from the perspective of a C# developer. Say having the following snippet in C#: var httpClient = new HttpClient(); var response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); How to write the same in F#? 回答1: Here is a function that should do what you're looking for (note that you'll have to wrap the code in an asynchronous computation expression in order to

'foreach' failing when using Parallel Task Library

大兔子大兔子 提交于 2020-01-01 18:59:25
问题 The following code creates the correct number of files, but every file contains the contents of the first list. Can anyone spot what I've done wrong please? private IList<List<string>> GetLists() { // Code omitted for brevity... } private void DoSomethingInParallel() { var lists = GetLists(); var tasks = new List<Task>(); var factory = new TaskFactory(); foreach (var list in lists) { tasks.Add(factory.StartNew(() => { WriteListToLogFile(list); })); } Task.WaitAll(tasks.ToArray()); } 回答1: The

Tracking progress of a multi-step Task

烈酒焚心 提交于 2020-01-01 12:22:12
问题 I am working on a simple server that exposes webservices to clients. Some of the requests may take a long time to complete, and are logically broken into multiple steps. For such requests, it is required to report progress during execution. In addition, a new request may be initiated before a previous one completes, and it is required that both execute concurrently (barring some system-specific limitations). I was thinking of having the server return a TaskId to its clients, and having the

how to use yield to return the collection of Item in parallel block or Task

谁都会走 提交于 2020-01-01 10:59:06
问题 I looking for help on how to make use of yield keyword to return IEnumberable in parallel blocks or Task block. Below is the pseudo code public IEnumerable<List<T>> ReadFile( ) { foreach (string filepath in lstOfFiles) { var stream = new FileStream(filepath , FileMode.Open, FileAccess.Read); foreach (var item in ReadStream(stream)) yield return item; //where item is of type List<string> } } I want to convert above code to parallel block like below lstOfFiles.AsParallel() .ForAll(filepath => {

How to get a task NOT to be executed on the UI thread

被刻印的时光 ゝ 提交于 2020-01-01 08:23:23
问题 The following code is a simplification of a code in a real application. The problem below is that a long work will be ran in the UI thread, instead of a background thread. void Do() { Debug.Assert(this.Dispatcher.CheckAccess() == true); Task.Factory.StartNew(ShortUIWork, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); } void ShortUIWork() { Debug.Assert(this.Dispatcher.CheckAccess() == true); Task.Factory.StartNew(LongWork,

How to get a task NOT to be executed on the UI thread

徘徊边缘 提交于 2020-01-01 08:22:09
问题 The following code is a simplification of a code in a real application. The problem below is that a long work will be ran in the UI thread, instead of a background thread. void Do() { Debug.Assert(this.Dispatcher.CheckAccess() == true); Task.Factory.StartNew(ShortUIWork, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); } void ShortUIWork() { Debug.Assert(this.Dispatcher.CheckAccess() == true); Task.Factory.StartNew(LongWork,