task-parallel-library

Workaround for issue in .NET 4.0 where SynchronizationContext.Current is null

我们两清 提交于 2020-01-09 10:32:06
问题 What is a workaround for the issue where the SynchronizationContext.Current is null unexpectedly on the main thread in .NET 4.0? See: SynchronizationContext.Current is null in Continuation on the main UI thread 回答1: I created several extension methods that matched ContinueWith and StartNew except that they also take an additional SyncronizationContext . I then use this argument to restore the expected SynchronizationContext before executing the action: Below, I've given examples: public

Write your own async method

被刻印的时光 ゝ 提交于 2020-01-09 08:33:51
问题 I would like to know how to write your own async methods the "correct" way. I have seen many many posts explaining the async/await pattern like this: http://msdn.microsoft.com/en-us/library/hh191443.aspx // Three things to note in the signature: // - The method has an async modifier. // - The return type is Task or Task<T>. (See "Return Types" section.) // Here, it is Task<int> because the return statement returns an integer. // - The method name ends in "Async." async Task<int>

Using ContinueWith with Multiple Tasks

纵然是瞬间 提交于 2020-01-07 06:18:51
问题 This is not behaving quite as I thought it would the need is simple, launch a number of tasks to do operations on an object. One unique object per task. The second part is a ContinueWith when each task reports the results. However, I am not getting a WhenAll type behavior. Hopefully someone can set me straight. _tasks = new Task<AnalysisResultArgs>[_beansList.Count]; for (int loopCnt = 0; loopCnt < _beansList.Count; loopCnt++) { _tasks[loopCnt] = Task<AnalysisResultArgs>.Factory.StartNew(() =

2 Async tasks in parallel and waiting for results - .Net

穿精又带淫゛_ 提交于 2020-01-07 03:09:11
问题 I intend to run two tasks in parallel and wait for both of them to finish. Here is my two tasks: Private Async Function tempWorker1() As Task For i = 1 To 5000 If i Mod 100 = 0 Then Console.WriteLine("From 1:{0}", i) End If Next End Function Private Async Function tempWorker2() As Task For i = 1 To 5000 If i Mod 100 = 0 Then Console.WriteLine("From 2:{0}", i) End If Next End Function I run them as: Dim task1 As Task = tempWorker1() Dim task2 As Task = tempWorker2() Await Task.WhenAll(task1,

Why is CPU usage constantly increasing after starting/stopping threads?

杀马特。学长 韩版系。学妹 提交于 2020-01-06 20:25:02
问题 I have a program where on a button's click, a new thread will be created (if it didn't exist already) and a connection to a camera is established. Now consider this same flow, but with N number of cameras (thus creating N threads on click). After the button is clicked again, all of the previously created threads are told to stop executing (through a boolean flag), and then Join(500) is called on each one - ending all threads. Now, I have noticed that successive clicks performed within a short

Task is being scheduled on the same thread as the caller's

浪子不回头ぞ 提交于 2020-01-06 13:57:15
问题 My application has a View Model which contains a Lazy<BitmapImage> field. The field is populated using a service call to the server. In cases where the image is large, it takes a few seconds for the server to return the image (which is in fact a byte[] ) therefore the UI is blocked. To prevent this, I put the service call in a Task , so that a background thread gets the image and then calls the OnPropertyChanged to let the UI know the image is returned: Console.WriteLine("Outside Task

“await task.ConfigureAwait(false)” versus “await ContextSwitcher.SwitchToThreadPool()” [closed]

允我心安 提交于 2020-01-06 05:36:06
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last month . It's widely recommended to use ConfigureAwait(false) like this: await Do1Async().ConfigureAwait(false); // ... await Do2Async().ConfigureAwait(false); // ... await Do3Async().ConfigureAwait(false); // ... IIRC, at the same time it's widely discouraged to use something like

Deadlock using Control.Invoke?

萝らか妹 提交于 2020-01-05 10:27:19
问题 I'm building an app using TPL in VS2010 Ultimate. The most of the times I run the app it becomes unresponsive when I Call DoRepresentation() from the UI's thread. void DoRepresentation() { Parallel.ForEach(cgs, loopOptions, g => { UpdateRepresentation(g); }); } void UpdateRepresentation(object g) { view.Invoke(new Action(() => { representation = new MyRepresentation(g); })); } I don't know why the app is becoming unresponsive. Am I having a deadlock? Inside MyRepresentation I do some calls to

Multiple Task Continuation

丶灬走出姿态 提交于 2020-01-05 08:38:24
问题 I would like to understand this scenario a little clearer: Consider the following code: frmProgressAsync prog = new frmProgressAsync(true); TaskWithProgress t = new TaskWithProgress("Smoothing CP", true); t.Task = A.ShowMovingAverage(tension,t.Progress) .ContinueWith(prev => { t.ProgressInformation.Report("Smoothing FG"); B.ShowMovingAverage(tension, t.Progress); }); await prog.RunAsync(t); I have two tasks I wish to run A.ShowMovingAverage and B.ShowMovingAverage . Both return a Task. In the

Async Web Service call not consistently asynchronous

巧了我就是萌 提交于 2020-01-05 08:28:09
问题 I'm having issues creating an asynchronous web service using the Task Parallel Library with ASP.NET Web API 2. I make an asynchronous call to a method StartAsyncTest and create a cancellation token to abort the method. I store the token globally and then retrieve it and call it from a second method CancelAsyncTest . Here is the code: // Private Global Dictionary to hold text search tokens private static Dictionary<string, CancellationTokenSource> TextSearchLookup = new Dictionary<string,