task

C# TPL how to know that all tasks are done?

最后都变了- 提交于 2019-12-20 13:33:13
问题 I have the Loop which generates tasks. Code: Task task = null; foreach (Entity a in AAAA) { // create the task task = new Task(() => { myMethod(a); }, Token, TaskCreationOptions.None); task.Start(); } As you can see in each iteration task object has new initialization (..new Task(() =>..) How can I know that all tasks done? 回答1: I f you replace this with a Parallel.ForEach(..., () => myMethod(a), ...) Then you get an automatic Wait on all tasks at the end of the ForEach. And maybe run the

Should I notice a difference in using Task vs Threads in .Net 4.0?

99封情书 提交于 2019-12-20 11:36:30
问题 I updated my code to use Tasks instead of threads.... Looking at memory usage and CPU I do not notices any improvements on the multi-core PC, Is this expected? My application essentially starts up threads/tasks in different objects when it runs... All I'm doing is a simple Task a = new Task(...) a.Start(); 回答1: There are various implications to using Tasks instead of Threads, but performance isn't a major one (assuming you weren't creating huge numbers of threads.) A few key differences: The

Android: How to make launcher always open the main activity instead of child activity? (or otherwise)

旧时模样 提交于 2019-12-20 10:43:31
问题 I have activities A and B. The A is the one with LAUNCHER intent-filter (i.e. the activity that is started when we click the app icon on home screen). A launches B using startActivity(new Intent(A.this, B.class)) . When the user has the B activity open, and then put my application into the background, and later my application's process is killed, when the user starts my application again, B is opened instead of A. This caused a force close in my app, because A is the activity that initializes

Waiting on a Task with a OnlyOnFaulted Continuation causes an AggregateException

☆樱花仙子☆ 提交于 2019-12-20 10:26:15
问题 I have some simple code as a repro: var taskTest = Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(5000); }).ContinueWith((Task t) => { Console.WriteLine("ERR"); }, TaskContinuationOptions.OnlyOnFaulted); try { Task.WaitAll(taskTest); } catch (AggregateException ex) { foreach (var e in ex.InnerExceptions) Console.WriteLine(e.Message + Environment.NewLine + e.StackTrace); } However, I'm getting an unexpected TaskCanceledException being thrown in the try catch block (it's in the

How to a synchronize tasks?

徘徊边缘 提交于 2019-12-20 10:11:59
问题 Say I have an async method which saves to file: async Task SaveToFileAsync() { var file = await folder.GetFileAsync ( ...) var stream = file.OpenFileAsync(...) ///etc } Now imagine that SaveToFileAsync is called twice simultaneously. This is a problem because you can't write on the same file simultaneously If this were a regular method, lock() would fix this: void SaveToFile() { lock(something) { /// code here } } However, lock is not allowed in an async method. Of course, one could call

Would a Task<T>.Convert<TResult> extension method be useful or does it have hidden dangers?

柔情痞子 提交于 2019-12-20 09:10:37
问题 I'm writing client libraries for Google Cloud APIs which have a fairly common pattern for async helper overloads: Do some brief synchronous work to set up a request Make an asynchronous request Transform the result in a simple way Currently we're using async methods for that, but: Transforming the result of await ends up being annoying in terms of precedence - we end up needing (await foo.Bar().ConfigureAwait(false)).TransformToBaz() and the brackets are annoying. Using two statements

Create scheduled task using VB.net [duplicate]

冷暖自知 提交于 2019-12-20 08:09:26
问题 This question already has answers here : Setting up a scheduled task in .Net (2 answers) Closed 5 years ago . How do I create a scheduled task using VB.NET - Populating Scheduled Task fields from vb.net program on button click? I have nothing at the moment, nor do I even know if it is possible. 回答1: You have to create wrappers around the native COM interfaces. If you don't want to do it yourself, you can use this library https://taskscheduler.codeplex.com using System; using Microsoft.Win32

Create scheduled task using VB.net [duplicate]

萝らか妹 提交于 2019-12-20 08:08:11
问题 This question already has answers here : Setting up a scheduled task in .Net (2 answers) Closed 5 years ago . How do I create a scheduled task using VB.NET - Populating Scheduled Task fields from vb.net program on button click? I have nothing at the moment, nor do I even know if it is possible. 回答1: You have to create wrappers around the native COM interfaces. If you don't want to do it yourself, you can use this library https://taskscheduler.codeplex.com using System; using Microsoft.Win32

C# Running many async tasks the same time

て烟熏妆下的殇ゞ 提交于 2019-12-20 07:36:12
问题 I'm kinda new to async tasks. I've a function that takes student ID and scrapes data from specific university website with the required ID. private static HttpClient client = new HttpClient(); public static async Task<Student> ParseAsync(string departmentLink, int id, CancellationToken ct) { string website = string.Format(departmentLink, id); try { string data; var stream = await client.GetAsync(website, ct); using (var reader = new StreamReader(await stream.Content.ReadAsStreamAsync(),

Can't execute the task in VS Code to run gulpfile.js

北城以北 提交于 2019-12-20 07:26:54
问题 Trying to follow roughly this guide (as many like this one). In tasks.json I've added the following, right next to build task. { "taskName": "gulpify", "args": [], "showOutput": "always", "isBuildCommand": true } In gulpfile.js I've added the following. var gulp = require("gulp"); //gulp.task("default", "gulpify"); gulp.task("gulpify", function () { alert("bzz"); console.log("bzz"); }); When I execute the task from the embedded workbench command line (not PowerShell but the internal that