How can I prevent synchronous continuations on a Task?

New in .NET 4.6: .NET 4.6 contains a new TaskCreationOptions: RunContinuationsAsynchronously. Since you’re willing to use Reflection to access private fields… You can mark the TCS’s Task with the TASK_STATE_THREAD_WAS_ABORTED flag, which would cause all continuations not to be inlined. const int TASK_STATE_THREAD_WAS_ABORTED = 134217728; var stateField = typeof(Task).GetField(“m_stateFlags”, BindingFlags.NonPublic | BindingFlags.Instance); stateField.SetValue(task, (int) stateField.GetValue(task) … Read more

What is the ‘realtime’ process priority setting for?

A realtime priority thread can never be pre-empted by timer interrupts and runs at a higher priority than any other thread in the system. As such a CPU bound realtime priority thread can totally ruin a machine. Creating realtime priority threads requires a privilege (SeIncreaseBasePriorityPrivilege) so it can only be done by administrative users. For … Read more

What is the best way to seed a database in Rails?

Updating since these answers are slightly outdated (although some still apply). Simple feature added in rails 2.3.4, db/seeds.rb Provides a new rake task rake db:seed Good for populating common static records like states, countries, etc… http://railscasts.com/episodes/179-seed-data *Note that you can use fixtures if you had already created them to also populate with the db:seed task … Read more

Promise equivalent in C#

In C#: Task<T> is a future (or Task for a unit-returning future). TaskCompletionSource<T> is a promise. So your code would translate as such: // var promise = new Promise<MyResult>; var promise = new TaskCompletionSource<MyResult>(); // handlerMyEventsWithHandler(msg => promise.Complete(msg);); handlerMyEventsWithHandler(msg => promise.TrySetResult(msg)); // var myResult = promise.Future.Await(2000); var completed = await Task.WhenAny(promise.Task, Task.Delay(2000)); if (completed == … Read more

Platform.runLater and Task in JavaFX

Use Platform.runLater(…) for quick and simple operations and Task for complex and big operations . Use case for Platform.runLater(…) Use case for Task: Task Example in Ensemble App Example: Why Can’t we use Platform.runLater(…) for long calculations (Taken from below reference). Problem: Background thread which just counts from 0 to 1 million and update progress … Read more

Timer & TimerTask versus Thread + sleep in Java

The advantage of TimerTask is that it expresses your intention much better (i.e. code readability), and it already has the cancel() feature implemented. Note that it can be written in a shorter form as well as your own example: Timer uploadCheckerTimer = new Timer(true); uploadCheckerTimer.scheduleAtFixedRate( new TimerTask() { public void run() { NewUploadServer.getInstance().checkAndUploadFiles(); } }, … Read more

Difference between OperationCanceledException and TaskCanceledException?

OperationCanceledException is simply the base class for TaskCanceledException – so if you catch the former, you’ll still catch the latter. Some operations on concurrent collections throw just OperationCanceledException, as there aren’t any actual tasks involved (at least as far as the public API is concerned). See BlockingCollection.TryTake for an example. I would catch the OperationCanceledException … Read more

A Task’s exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was

If you create a Task, and you don’t ever call task.Wait() or try to retrieve the result of a Task<T>, when the task is collected by the garbage collector, it will tear down your application during finalization. For details, see MSDN’s page on Exception Handling in the TPL. The best option here is to “handle” … Read more

Task.Run with Parameter(s)?

private void RunAsync() { //Beware of closures. String is immutable. string param = “Hi”; Task.Run(() => MethodWithParameter(param)); } private void MethodWithParameter(string param) { //Do stuff } Edit Due to popular demand I must note that the Task launched will run in parallel with the calling thread. Assuming the default TaskScheduler this will use the .NET … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)