task

About the Task.StartNew(Action<Object>, Object) method

你离开我真会死。 提交于 2019-12-05 05:36:39
问题 I'm learning the TPL on this page, and one code block confuses me a lot. I was reading this page: Task Parallelism (Task Parallel Library) in one section, it said that the following code is the right solution because a lambda in a loop can't get the value as it mutates after each iteration, but the final value. So you should wrap the "i" in a CustomData object. The code is below: class CustomData { public long CreationTime; public int Name; public int ThreadNum; } public class Example {

How to check that all tasks have been properly completed?

别说谁变了你拦得住时间么 提交于 2019-12-05 05:18:13
I have the following lines in my code: var taskA = Task.Factory.StartNew(WorkA); var taskB = Task.Factory.StartNew(WorkB); var allTasks = new[] { taskA, taskB }; Task.Factory.ContinueWhenAll(allTasks, tasks => FinalWork(), TaskContinuationOptions.OnlyOnRanToCompletion); But when I run this, I get the following error: It is invalid to exclude specific continuation kinds for continuations off of multiple tasks. Which is caused by the option TaskContinuationOptions.OnlyOnRanToCompletion . My question is how to check that all tasks have done their work properly (all tasks statuses are

Update UI before running the rest of method

不羁的心 提交于 2019-12-05 04:45:59
I have a long-running operation that have to be done in UI thread (involves UI elements that cannot be freezed). I want to display a busy indicator before running the operation. busyIndicator.Visibility = Visibility.Visible; LongRunningMethod(); busyIndicator.Visibility = Visibility.Collapsed; Of course this does not work because rendering does not occur until the operation finishes. I tried to use Task.Yield() to run the rest of method asynchronously: busyIndicator.Visibility = Visibility.Visible; await Task.Yield(); LongRunningMethod(); This also does not work, as far as I understand,

C# / VB.Net Task vs Thread vs BackgroundWorker

拜拜、爱过 提交于 2019-12-05 04:28:37
I have "Googled" but still confused with Task, Thread, and Background Worker..... Is "Task is a high level API that running on current thread" correct ? If 1 is correct, why I need to use invoke to change the UI inside the task at same thread ? Backgroundworker only got lowest priority in the application ? So the performance of backgroundworker is lower than task and thread ? Right ? Finally, in my application I need to get a string from server using "HttpWebRequest", after that parse the string and update the UI. If I use "HttpWebRequest.BeginGetResponse" to wait for the async result and

Force Task<T> to different core ?

时间秒杀一切 提交于 2019-12-05 03:57:24
问题 Tpl and Plinq automatically assign work to threads (at core/s... { well ,if #threads> #cores then , >1 thread will run on the same core. }). However , Let's say I have MyMethod1(){..} and MyMethod2(){..} and I need to make sure (!) that each will run on a different core! (e.g. intensive computing) The nearest solution I found is Plinq's .WithExecutionMode (ParallelExecutionMode.ForceParallelism) But this is for a different situation where Plinq might think that it is better to do it

How to manage properly an exception in a Task with ContinueWith

廉价感情. 提交于 2019-12-05 03:28:58
After reading information about task and exepcion management, I am using this code to manage an exception thrown in a Task: Task<Object> myTask = Task.Factory.StartNew<Object>(doTask, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default); myTask .ContinueWith(task => afterTask(task), TaskScheduler.FromCurrentSynchronizationContext()); Where doTask and AfterTask are: private <Object> doTask() { throw new Exception("BOOM"); } private afterTask(Task<Object> aTask) { if (aTask.IsFaulted) { MessageBox.Show(aTask.Exception.InnerException.Message); } else //whatever } When

C# Async Task Method Without Await or Return

主宰稳场 提交于 2019-12-05 03:03:14
I have an Interface I that is implemented in two places like: interface I { Task DoSomething(); } The interface has async Task DoSomething method API that is then implemented in class A like: class A : I {....} class B : I {....} In class A, the implementation of DoSomething is like below and that is OK: public async Task DoSomething() { if (...) { await DoIt(); } } However, in class B, the implementation of DoSomething() should not do anything. So, its implementation looks like this: public async Task DoSomething() { // nothing } This compiles but I am not sure how correct it is do do

Android 第六课——Activity高级

 ̄綄美尐妖づ 提交于 2019-12-05 02:47:55
Activity 生命周期: 生命周期7个方法的调用时机: 1)onCreate:第一次创建这个Activity时, 也就是系统中没有缓存当前的Activity时,这个方法首先被调用。调用之后这个Activity就会被压入所谓的Android Task栈中缓存起来,下次用时出栈就可以。所以,为了更加节约资源,我们一般把Activity所对应的layout中拥有的组件首先使用private作为这个Activity的私有成员,然后在onCreate方法中初始化,这样只要在Activity创建的时候,初始化一次组件就够了。 2)onstart:当这个Activity成为用户可见状态时, 也就是在手机界面上正确显示的时候这个方法会被调用。所以,如果一个Activity之前已经创建好了,下次再次调用时(比如返回按钮)就会从Task栈中获取直接返回给用户,那么就不会再调用onCreate了,而是先调用onRestart,然后等到用户可见状态时调用onStart。 3)onResume:当这个Activity成为用户可见状态而且用户可以获取焦点时, 也就是在onStart之后,当这个视图可以与用户交互时这个方法会被调用。这个方法调用完成之后,整个Activity就是处于运行状态了。 4)onPause:当一个Activity正在使用,这时另一个Activity开始启动

Grunt & requirejs optimizer for a multi app project

落花浮王杯 提交于 2019-12-05 02:16:46
I'm having issues getting Grunt to perform requirejs optimization on a project with the following structure: static/js |── apps |── app.js |── dash.js |── news.js ... (many more 'app' files) |── build |── collections |── libs |── models |── util |── views Each of static/js/apps/*.js should be compiled to static/js/build/*.js containing the relevant dependencies (eg. views/view1 , libs/query etc). This is currently being performed by a basic bash script: JS_ROOT="static/js" for f in ${JS_ROOT}/apps/* do FILE=$(basename -s .js ${f}) pushd . cd ${JS_ROOT} && r.js -o baseUrl=. name=libs/require

Launch a subdag with variable parallel tasks in airflow

ぃ、小莉子 提交于 2019-12-05 02:09:07
问题 I have an airflow workflow that I'd like to modify (see illustration at the bottom). However, I couldn't find a way to do that in the docs. I've looked at subdags, branching and xcoms without luck. There doesn't seem to be a way to specify how many tasks should run in parallel in a subdag based on a return from an operator. To add to the problem, each task in the subdag receives a different parameter (an element from the list returned by the previous operator) This is an illustration of what