task

update UI in Task using TaskScheduler.FromCurrentSynchronizationContext

≡放荡痞女 提交于 2019-12-17 17:11:01
问题 I want to add some text to list box using Task and I simply use a button and place in click event this code: TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(() => { for (int i = 0; i < 10; i++) { listBox1.Items.Add("Number cities in problem = " + i.ToString()); System.Threading.Thread.Sleep(1000); } }, CancellationToken.None, TaskCreationOptions.None, uiScheduler); but it does not work and UI locked until the end of the for loop. Where is

Why Thread.Sleep affects creation of new Tasks?

爷,独闯天下 提交于 2019-12-17 17:00:36
问题 private static void Main(string[] args) { for (int i = 0; i < 1000; i++) { Task.Factory.StartNew(() => { Thread.Sleep(1000); Console.WriteLine("hej"); Thread.Sleep(10000); }); } Console.ReadLine(); } Why this code won't print 1000 times "hej" after one second? Why Thread.Sleep(10000) has an impact on code behavior? 回答1: Factory.StartNew effectively delegates the work to ThreadPool . Threadpool will create number of threads immediately to respond the request as long as threads count is less

When does a C# Task actually start?

↘锁芯ラ 提交于 2019-12-17 15:51:48
问题 When does a Task actually start? public void DoSomething() { Task myTask = DoSomethingAsync(); Task.WaitAll(new[] { myTask }, 2000); } public async Task DoSomethingAsync() { await SomethingElse(); } Does it start immediately when initializing it in Task myTask = DoSomethingAsync(); or does it start when you say to wait for it in Task.WaitAll(new[] { myTask }, 2000); ? 回答1: Calling an async method returns a hot task, a task that has already been started. So there is no actual code necessary to

When does a C# Task actually start?

耗尽温柔 提交于 2019-12-17 15:51:11
问题 When does a Task actually start? public void DoSomething() { Task myTask = DoSomethingAsync(); Task.WaitAll(new[] { myTask }, 2000); } public async Task DoSomethingAsync() { await SomethingElse(); } Does it start immediately when initializing it in Task myTask = DoSomethingAsync(); or does it start when you say to wait for it in Task.WaitAll(new[] { myTask }, 2000); ? 回答1: Calling an async method returns a hot task, a task that has already been started. So there is no actual code necessary to

Using async keyword in method signature to return a Task in Web Api endpoint

南楼画角 提交于 2019-12-17 14:47:54
问题 If I wanted to write a non-blocking web api action by returning a Task object, I could do it with or without using the async keyword as such: Using async public async Task<HttpResponseMessage> Get() { Func<HttpResponseMessage> slowCall = () => { Thread.Sleep(2000); return Request.CreateResponse(HttpStatusCode.OK, "Hello world"); }; var task = Task<HttpResponseMessage>.Factory.StartNew(slowCall); return await task; } Without using async public Task<HttpResponseMessage> Get() { Func

Using async keyword in method signature to return a Task in Web Api endpoint

十年热恋 提交于 2019-12-17 14:46:03
问题 If I wanted to write a non-blocking web api action by returning a Task object, I could do it with or without using the async keyword as such: Using async public async Task<HttpResponseMessage> Get() { Func<HttpResponseMessage> slowCall = () => { Thread.Sleep(2000); return Request.CreateResponse(HttpStatusCode.OK, "Hello world"); }; var task = Task<HttpResponseMessage>.Factory.StartNew(slowCall); return await task; } Without using async public Task<HttpResponseMessage> Get() { Func

Running tasks parallel in powershell

≡放荡痞女 提交于 2019-12-17 09:40:17
问题 I have a PowerShell script like this: Foreach ($file in $files) { [Do something] [Do something] [Do something] } This way one file is treated after the other. I want to treat 4 files at the same time. I know of the foreach -parallel loop, but that does the [do something] tasks in parallel. I basically want to run the whole foreach loop in parallel. How can I achieve this in PowerShell? 回答1: You might look into Jobs or runspaces. Here is an example of Jobs: $block = { Param([string] $file) "

How do task killers work?

佐手、 提交于 2019-12-17 08:24:40
问题 The usefullness of task killer apps is debated, but I'm wondering: how do they actually work? How is it possible to kill particular process? Is there an API for this, and if so what does it actually do ? EDIT Worth adding: I saw task killer apps kill processes on not rooted devices . So, I wonder how is it possible to kill process, which you don't own in Android? 回答1: In a nutshell, Automatic Task Killers work by polling the OS for a list of currently running processes and the memory they are

caching the result from a [n async] factory method iff it doesn't throw

假装没事ソ 提交于 2019-12-17 06:51:08
问题 UPDATE: Heavily revised after @usr pointed out I'd incorrectly assumed Lazy<T> 's default thread safety mode was LazyThreadSafetyMode.PublicationOnly ... I want to lazily compute a value via an async Factory Method (i.e. it returns Task<T> ) and have it cached upon success. On exception, I want to have that be available to me. I do not however, want to fall prey to the exception caching behavior that Lazy<T> has in its default mode ( LazyThreadSafetyMode.ExecutionAndPublication ) Exception

Retry a task multiple times based on user input in case of an exception in task

瘦欲@ 提交于 2019-12-17 05:03:58
问题 All the service calls in my application are implemented as tasks.When ever a task is faulted ,I need to present the user with a dialog box to retry the last operation failed.If the user chooses retry the program should retry the task ,else the execution of the program should continue after logging the exception.Any one has got a high level idea on how to implement this functionality ? 回答1: UPDATE 5/2017 C# 6 exception filters make the catch clause a lot simpler : private static async Task<T>