Task continuation on UI thread

Call the continuation with TaskScheduler.FromCurrentSynchronizationContext(): Task UITask= task.ContinueWith(() => { this.TextBlock1.Text = “Complete”; }, TaskScheduler.FromCurrentSynchronizationContext()); This is suitable only if the current execution context is on the UI thread.

Set ApartmentState on a Task

When StartNew fails you just do it yourself: public static Task<T> StartSTATask<T>(Func<T> func) { var tcs = new TaskCompletionSource<T>(); Thread thread = new Thread(() => { try { tcs.SetResult(func()); } catch (Exception e) { tcs.SetException(e); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); return tcs.Task; } (You can create one for Task that will look almost identical, or add … 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

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

How can I call an async method in Main?

Your Main method can be simplified. For C# 7.1 and newer: static async Task Main(string[] args) { test t = new test(); await t.Go(); Console.WriteLine(“finished”); Console.ReadKey(); } For earlier versions of C#: static void Main(string[] args) { test t = new test(); t.Go().Wait(); Console.WriteLine(“finished”); Console.ReadKey(); } This is part of the beauty of the async … Read more

How to safely call an async method in C# without await

If you want to get the exception “asynchronously”, you could do: MyAsyncMethod(). ContinueWith(t => Console.WriteLine(t.Exception), TaskContinuationOptions.OnlyOnFaulted); This will allow you to deal with an exception on a thread other than the “main” thread. This means you don’t have to “wait” for the call to MyAsyncMethod() from the thread that calls MyAsyncMethod; but, still allows you … Read more

JavaFX2: Can I pause a background Task / Service?

Solution When your background process encounters a situation where it requires a user to be prompted for input, use FutureTask executed in Platform.runLater to showAndWait the dialog prompt on the JavaFX application thread. In the background process use futureTask.get to pause the background process until the user has input the necessary values which will allow … Read more

Android: Cancel Async Task

From SDK: Cancelling a task A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns. To ensure that a task is cancelled as quickly as possible, you should … Read more