task

Task is ignoring Thread.Sleep

梦想与她 提交于 2019-12-05 17:31:31
trying to grasp the TPL. Just for fun I tried to create some Tasks with a random sleep to see how it was processed. I was targeting a fire and forget pattern.. static void Main(string[] args) { Console.WriteLine("Demonstrating a successful transaction"); Random d = new Random(); for (int i = 0; i < 10; i++) { var sleep = d.Next(100, 2000); Action<int> succes = (int x) => { Thread.Sleep(x); Console.WriteLine("sleep={2}, Task={0}, Thread={1}: Begin successful transaction", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, x); }; Task t1 = Task.Factory.StartNew(() => succes(sleep)); } Console

iOS background process similar to Android AlarmManager

只愿长相守 提交于 2019-12-05 16:45:50
问题 I've to schedule a fixed task that check some info from internet at fixed interval timer. Into Android, I use AlarmManager with setRepeating, but I'm newbie to iOS. There are some similar api that help me? I need a task that survive when app is killed by operating system. Do you know a tutorial about it? 回答1: AFAIK in iOS you cannot implement such service that will stay alive after its host app is killed, also only several types of applications can run some background tasks - see

How to get the information of transitive dependencies in a gradle task?

匆匆过客 提交于 2019-12-05 16:21:40
I want to get information of all dependencies (including transitive ones) in a gradle task. I tried the code: class MyGradlePlugin implements Plugin<Project> { void apply(Project project) { project.afterEvaluate { println " Project:" + project.name project.configurations.each { conf -> println " Configuration: ${conf.name}" conf.allDependencies.each { dep -> println " ${dep.group}:${dep.name}:${dep.version}" } } } } } But it only prints the declared ones, no transitive ones. That means, if my dependencies is: dependencies { compile "com.google.guava:guava:18.0" compile 'org.codehaus.groovy

Synchronizing events from different threads in console application

青春壹個敷衍的年華 提交于 2019-12-05 15:22:07
I am feeling like a total noob asking this, but anyway, here it goes: I was wondering what the easiest way of synchronizing events from different threads is. Some sample code: class Program { static void Main(string[] args) { Console.WriteLine("# started on:" + Thread.CurrentThread.ManagedThreadId); tt t = new tt(); t.First += new EventHandler(t_First); t.Second += new EventHandler(t_Second); Task task = new Task(new Action(t.Test)); task.Start(); while (true) { Console.ReadKey(); Console.WriteLine("# waiting on:" + Thread.CurrentThread.ManagedThreadId); } } static void t_Second(object sender,

Run rake task from outside RAILS_ROOT

房东的猫 提交于 2019-12-05 12:10:33
My RAILS_ROOT is /usr/local/www/application/ If I run 'rake db:migrate RAILS_ENV=production" from within the RAILS_ROOT it works fine. However I can't seem to find a way to run the same command from outside the RAILS_ROOT. Try: rake -f $RAILS_ROOT/Rakefile db:migrate RAILS_ENV=production # Assuming you set the environment variable. # Else, just replace $RAILS_ROOT by actual value I think you need to re-think your question. When running rake without specifying a rakefile, it's going to search in the current directory for said rakefile. In a directory besides RAILS_ROOT, it's going to find no

Using Interlocked.CompareExchange to increment a counter until a value

会有一股神秘感。 提交于 2019-12-05 11:55:35
I need to increment a counter until it reaches a particular number. I can use two parallel task to increment the number. Instead of using a lock to check if the number has not reach the maximum allowed value and then incrementing, I thought using Interlocked .CompareExchange in the following manner: public class CompareExchangeStrategy { private int _counter = 0; private int _max; public CompareExchangeStrategy(int max) { _max = max; } public void Increment() { Task task1 = new Task(new Action(DoWork)); Task task2 = new Task(new Action(DoWork)); task1.Start(); task2.Start(); Task[] tasks = new

Silent exceptions in Task.Factory.StartNew when using a long running background consumer task?

て烟熏妆下的殇ゞ 提交于 2019-12-05 10:55:40
This notifies of an unhandled exception: new Thread(_ => { throw new Exception(); }).Start(); This does not (at least until you wait/retrieve the result): Task.Factory.StartNew(() => { throw new Exception(); }); Why? What has happened to the thread on which the exception was thrown? Does it die? This is a problem where you run a task but don't need its result, or need to wait on it, like this: _operationQueue = new BlockingCollection<Operation>(); Task.Factory.StartNew(() => { foreach (var item in _operationQueue.GetConsumingEnumerable()) { // do something that throws } }, TaskCreationOptions

WPF asynchronous Task<T> blocking UI

青春壹個敷衍的年華 提交于 2019-12-05 10:50:31
I already worked with Task type. And all was good while Task return nothing. For example: XAML: <Button Name="_button" Click="ButtonBase_OnClick"> Click </Button> CodeBehind: private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { _button.IsEnabled = false; Task.Factory.StartNew(() => { Thread.Sleep(5*1000); Dispatcher.Invoke(new Action(() => _button.IsEnabled = true)); }); } This works fine. But I want to Task returns some value, for example Boolean . So I need to use Task<Boolean> : private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { _button.IsEnabled = false; var

Task.ContinueWith not working how I expected

試著忘記壹切 提交于 2019-12-05 09:35:04
Consider the following code. I am starting with a task that does nothing, and then using ContinueWith() to start 10 calls to a method that increments a counter. When I run this program, it prints "0", indicating that the increment() method hasn't been called at all. I was expecting it to be called 10 times, since that's how many times I called ContinueWith(). If I uncomment the "Thread.Sleep(20)" line, then it prints "10" as expected. This happens in either release or debug mode. My system is a core 2 quad with hyperthreading (8 logical cores) running Windows 7 x64. I assume I have some kind

What should an async method do if a Task is conditionally executed?

不问归期 提交于 2019-12-05 09:14:57
Suppose I have a method that awaits a Task. This method also returns a Task. For example: public async virtual Task Save(String path) { if (NewWords.Any()) { await FileManager.WriteDictionary(path, NewWords, true); } else await Task.Run(() => { }); } Is the else await Task.Run(() => { }); necessary here or am I free to leave it? Is there any difference if it is present/absent? Maybe there is some other approach to this I should take? It's worse than unnecessary, as you're spinning up a thread to do nothing and then waiting until after its finished doing nothing. The simplest way to do nothing,