---
title: "What is Task continuation on UI thread?"  
description: "What is Task continuation on UI thread?"  
author: "Anonymous User"  
published: 2014-10-10  
updated: 2023-07-04  
canonical: https://www.mindstick.com/forum/2356/what-is-task-continuation-on-ui-thread  
category: "oops"  
tags: ["oops"]  
reading_time: 3 minutes  

---

# What is Task continuation on UI thread?

Is there a '[standard](https://www.mindstick.com/articles/23223/naming-convention-or-coding-standard)' way to specify that a [task](https://www.mindstick.com/forum/161761/what-is-the-difference-between-task-and-thread) continuation should run on the thread from which the initial task was created?

Currently, I have the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) below - it is working but keeping track of the dispatcher and creating a [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) [Action](https://www.mindstick.com/forum/155752/what-is-action-result-with-its-types) seems like unnecessary overhead.

```
dispatcher = Dispatcher.CurrentDispatcher;            Task task = Task.Factory.StartNew(() =>            {                DoLongRunningWork();            });             Task UITask= task.ContinueWith(() =>            {                dispatcher.Invoke(new Action(() =>                {                    this.TextBlock1.Text = "Complete";                 }            });
```

## Replies

### Reply by Aryan Kumar

Sure. Task continuation on the UI thread is a way to run a task on the UI thread after another task has finished. This can be useful for tasks that need to update the UI, such as displaying a message or changing the value of a control.

To run a task continuation on the UI thread, you can use the `runOnUiThread()` method. This method takes a runnable object as a parameter. The runnable object is a task that will be run on the UI thread.

Here is an example of how to run a task continuation on the UI thread:

Java

```plaintext
import android.os.Handler;
import android.os.Looper;

public class TaskContinuation {

    public static void main(String[] args) {
        // Create a handler that will run the task on the UI thread
        Handler handler = new Handler(Looper.getMainLooper());

        // Create a task that will be run on the UI thread
        Runnable task = new Runnable() {
            @Override
            public void run() {
                // Do something on the UI thread
                System.out.println("This is running on the UI thread");
            }
        };

        // Run the task on the UI thread
        handler.post(task);
    }
}
```

This code will first create a handler that will run the task on the UI thread. Then, it will create a task that will be run on the UI thread. Finally, it will run the task on the UI thread.

To run the code, you can save it as a Java file and then compile and run it from the command line. For example, if you save the code as `TaskContinuation.java`, you can compile and run it by typing the following commands into the command line:

Code snippet

```plaintext
javac TaskContinuation.java
java TaskContinuation
```

The code will then print the following message to the console:

Code snippet

```plaintext
This is running on the UI thread
```

### Reply by Anonymous User

Call the continuation with TaskScheduler.FromCurrentSynchronizationContext():\
\

```
Task UITask = task.ContinueWith(() =>            {                this.TextBlock1.Text = "Complete";            }, TaskScheduler.FromCurrentSynchronizationContext());
```


---

Original Source: https://www.mindstick.com/forum/2356/what-is-task-continuation-on-ui-thread

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
