Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

Is the UI thread a ThreadPool thread?

Foo()
{
    Task.Run Bar();
}

Open in new window

Is it possible Bar() may run on the UI thread?
The documentation says Task.Run() queues Bar() to run on a ThreadPool thread.
Is the UI thread a ThreadPool thread?
If Foo() was run on the UI thread, is it possible Bar() might also run on the UI thread?
Avatar of kaufmed
kaufmed
Flag of United States of America image

The thing to understand about threads is that there are things called "synchronization contexts" that all threads have. Such contexts describe the environment that the thread is running in.

So the short answer to your question is:  The physical thread that is running your UI and the physical thread that is running Bar may be the same, but that doesn't mean that the code for each is running at the same time. The synchronization context will get updated appropriately as work is assigned to a thread. You shouldn't have to worry about spawning new threads and those threads freezing your UI.
a new thread - regardless whether it is a worker thread or a UI thread - cannot run in the context of the (main) UI thread. if it is an UI thread itself, it needs to run its own message loop. otherwise there is  a dead-lock or crash. the thread cannot use the message loop of the main UI thread without violating thread-safety. however, worker or UI thread could use postmessage calls to safely place messages into the main message queue.

if you want to show a progress bar, the control must be created in the context of the main UI thread. an asynchronously running thread then could provide the progress data, e. g. a shared integer that  is  the progress level to be shown in the bar. you probably would use a timer to refresh the progress bar - say - any 5 seconds. if the worker thread would be the only thread which writes to the shared progress variable, it is absolutely thread-safe.

Sara
Avatar of deleyd

ASKER

I should clarify my question, which is, is it possible Bar() might run on the UI thread, or is that an impossibility?
Task.Run will request a thread from the thread pool. This will not be the UI thread. You will need to marshal UI updates back to the UI thread using Invoke.

Threads queued to the thread pool are background threads. The UI thread is a foreground thread.
is it possible Bar() might run on the UI thread

you could call Bar() in a timer event:

using System;
using System.Timers;

public class BarTimer
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;   

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program at any time... ");
        Console.ReadLine();
    }

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("Calling Bar(0) at {0}", e.SignalTime);
        Bar();
    }
}

Open in new window


note, the BarTimer class could hold static variables for the progress made.

Sara
Avatar of deleyd

ASKER

Sorry my question is more theoretical. I was wondering in principle if i ran the program a million times, is it possible at least one of those times Bar() would run on the UI thread?

Or is that impossible because the UI thread is not one of the threads that Task.Run would ever use?
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deleyd

ASKER

Threadpool threads are always worker threads. -- Thank you that's what I needed to know! I appreciate the time spent answering this question. ~David