Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Find UI thread in UWP

I am Trying to Understand How Threading Works in UWP.

The Knowledge that I have is:

1) Each Application Window will have an UI thread. And many Worker(or Background) threads.

2) UI elements can be Accessed only through UI Thread. One can Do all the Memory Intensive Computations in Background Threads and Just Assign the Values to UI thread to Keep UI Responsive.

For this, CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(()=> method) is Used.

3) To Check Whether the thread is UI thread or a Non-UI thread, CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess can be used.(As Mentioned here

But,

When I try to use CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess, It Returns True in Most of the cases.(Even from Worker Threads Category)

Also,while Debugging in Thread Window, Under Category column There is a Main Thread which has only one Thread as shown here:

enter image description here

EDIT: My Colleague said that If Name is Worker Thread then its Non GUI thread.

When I use Observable.Start(From System.Reactive) instead of Task.Run, I get two Such Instances(with no-name), which defies one UI Thread Per window Concept :(

enter image description here

The Question is,

How can I Find the One Ui thread from the List? or..Is Main Thread the UI Thread?(But its Managed ID is 0)

Any Leads on the above Questions will be Really Helpful!

like image 631
cvcnsprakash Avatar asked Feb 24 '17 06:02

cvcnsprakash


People also ask

What is UI thread in WPF?

The UI thread queues work items inside an object called a Dispatcher. The Dispatcher selects work items on a priority basis and runs each one to completion. Every UI thread must have at least one Dispatcher, and each Dispatcher can execute work items in exactly one thread.

How do I change the UI of a worker thread?

In this case, to update the UI from a background thread, you can create a handler attached to the UI thread, and then post an action as a Runnable : Handler handler = new Handler(Looper. getMainLooper()); handler. post(new Runnable() { @Override public void run() { // update the ui from here } });

What is UI thread in Android?

User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.

What is a UI thread C#?

A UI thread creates UI elements and waits and responds to events like mouse clicks and key presses. You can only access the UI elements from the UI thread. There are two types of threads: background and foreground. A UI thread is an example of a foreground thread.


1 Answers

In (2) in the posting, using the dispatcher Run Async does not create a worker (thread pool) task. It creates a new task on the dispatcher context, which is a context containing just the UI thread.

Note that asynchrony and threading are different concepts. The task created by dispatcher Run Async is run asynchronously but on the same thread. That's probably why HasThreadAccess is returning true.

To create a task on a worker (thread pool) thread, you can use Task.Run. Note the thread-pool context has multiple threads, and these can be assigned to different processors, so it is possible that two tasks may run at the same time. Therefore, some firm of locking (such as SemaphoreSlim) is often needed.

sjb

like image 84
sjb-sjb Avatar answered Oct 14 '22 12:10

sjb-sjb