Questions tagged [ui-thread]

In some frameworks there's a dedicated thread that exclusively manages all UI objects. It's the developer's responsibility to use this thread for UI updates (e.g. adding, updating and removing controls).

In some frameworks (.Net, Android, iOS, Java Swing etc.) there's a dedicated thread that exclusivly creates and manages all the UI objects. Only this thread is allowed to modify UI objects (add, remove, set text on the button, etc). Other threads usually just post a request to make such modifications.

UI thread must never be paused for long with slow task (like communication over network or something computation intensive) as this would make all GUI non-responsive.

References

580 questions
1077
votes
29 answers

Can't create handler inside thread that has not called Looper.prepare()

What does the following exception mean; how can I fix it? This is the code: Toast toast = Toast.makeText(mContext, "Something", Toast.LENGTH_SHORT); This is the exception: java.lang.RuntimeException: Can't create handler inside thread that has not…
michael
  • 93,094
  • 111
  • 230
  • 334
168
votes
12 answers

How do we use runOnUiThread in Android?

I'm new to Android and I'm trying to use the UI-Thread, so I've written a simple test activity. But I think I've misunderstood something, because on clicking the button - the app does not respond anymore public class TestActivity extends Activity…
user1049280
  • 4,946
  • 8
  • 31
  • 51
83
votes
3 answers

What is the Android UiThread (UI thread)

Can someone explain to me what exactly the UI thread is? On developer.android.com it says about the runOnUiThread function public final void runOnUiThread (Runnable action) Since: API Level 1 Runs the specified action on the UI thread. If the …
user434885
  • 1,900
  • 6
  • 28
  • 49
35
votes
3 answers

Difference between Handler.post(Runnable r) and Activity.runOnUiThread(Runnable r)

Is there a difference between new Handler.post(Runnable r); and activity.runOnUiThread(Runnable r)
VinceFR
  • 2,415
  • 1
  • 18
  • 27
34
votes
3 answers

OperationQueue.main vs DispatchQueue.main

When you need to perform something on the main thread in the completion block of a networking task or an operation, which of these ways to get it would be the most appropriate and why?: OperationQueue.main.addOperation DispatchQueue.main.async
AppsDev
  • 11,441
  • 20
  • 81
  • 163
31
votes
3 answers

How to update UI in coroutines in Kotlin 1.3

I'm trying to call an API and when my variables are ready, update UI components respectively. This is my Network singleton who is launching the coroutine: object MapNetwork { fun getRoute(request: RoutesRequest, success:…
Mohsen
  • 1,620
  • 3
  • 15
  • 22
23
votes
3 answers

Android ViewPager automatically change page

I want to schedule an action to change automatically my ViewPager pages. I've tried: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... swipeTimer = new Timer(); …
GuilhE
  • 10,723
  • 13
  • 63
  • 94
22
votes
1 answer

Is a good practice create anonymous AsyncTask for parallel small known freeze process?

E.g.: you gonna do something that will take a few seconds and don't wanna freeze your UI thred, right? You could use an AsyncTask but you don't wanna create a external (or inner) class to solve a small freeze problem. So, is a good pratice do…
xpto
  • 2,521
  • 4
  • 27
  • 59
22
votes
4 answers

How to know if this thread is a UI Thread

Is there any way on Android to know, if the thread running my code, is the UI Thread or not ? In swing there was SwingUtilities.isEventDispatchThread() to tell me if i am on the UI Thread, or not. Is there any function in the Android SDK that lets…
user1730789
  • 4,969
  • 6
  • 32
  • 54
21
votes
7 answers

android - calling ui thread from worker thread

Hi I want to make Toast available to me no-matter-what and available from any thread whenever I like within my application. So to do this I extended the Activity class: import android.app.Activity; import android.os.Bundle; import…
conners
  • 1,400
  • 4
  • 16
  • 27
19
votes
5 answers

Is it possible to use AsyncTask in a Service class?

Everything is in the title. On the official documentations it is stated that Note that services, like other application objects, run in the main thread of their hosting process and AsyncTask only works if it is executed in the UIThread. So is it…
Spredzy
  • 4,702
  • 13
  • 47
  • 67
18
votes
2 answers

How to update UI from Android service using RxJava/RxAndroid

I have a Bound Service which responsible for downloading files and thus it knows the downloading status/progress. And the UI (Fragment or Activity) has to show/update download progress from the service. Actually i think the common way is to use…
18
votes
3 answers

Is it possible to initialize WPF UserControls in different threads?

We are developing a WPF application which will open a number of reports at the same time (just like a typical MDI application such as Excel or Visual Studio). Although it is possible to have the data context for those reports run in multiple worker…
tete
  • 4,389
  • 9
  • 44
  • 70
17
votes
3 answers

How do I test Prism event aggregator subscriptions, on the UIThread?

I have a class, that subscribes to an event via PRISMs event aggregator. As it is somewhat hard to mock the event aggregator as noted here, I just instantiate a real one and pass it to the system under test. In my test I then publish the event via…
Thorsten Lorenz
  • 11,293
  • 6
  • 48
  • 58
16
votes
1 answer

Does react-native support Multithreading and Background threading or Parallel Execution? How can we do that?

I have gone through the official documentation of react-native and some other medium sources and blogs, I came to know that there is UI Thread and JavaScript Thread in react-native. Javascript thread is the thread where the logic will run javascript…
1
2 3
38 39