How to Check If Current Thread Is Not Main Thread

How to check if current thread is not main thread

Looper.myLooper() == Looper.getMainLooper()

if this returns true, then you're on the UI thread!

Is it necessary to check whether the current thread is the main thread or not before dispatching to it asynchronously?

But do i really need to check the current Thread like someone did in the following Snippet

Not really. If you're already on the main thread, then when your code comes to an end, your dispatch_async block will be dispatched. So there might be a slight delay because we need the current run loop / transaction to come to an end, but it won't be noticeable.

I use dispatch_async to the main thread all the time as a way of making sure I'm on the main thread in situations where I'm uncertain (KVO callbacks, for example). No harm, no foul. It's just a form of insurance.

Check whether or not the current thread is the main thread

Have a look at the NSThread API documentation.

There are methods like

- (BOOL)isMainThread

+ (BOOL)isMainThread

and + (NSThread *)mainThread

Check if current thread is main thread, in Python

The problem with threading.current_thread().name == 'MainThread' is that one can always do:

threading.current_thread().name = 'MyName'
assert threading.current_thread().name == 'MainThread' # will fail

Perhaps the following is more solid:

threading.current_thread().__class__.__name__ == '_MainThread'

Having said that, one may still cunningly do:

threading.current_thread().__class__.__name__ = 'Grrrr'
assert threading.current_thread().__class__.__name__ == '_MainThread' # will fail

But this option still seems better; "after all, we're all consenting adults here."

UPDATE:

Python 3.4 introduced threading.main_thread() which is much better than the above:

assert threading.current_thread() is threading.main_thread()

UPDATE 2:

For Python < 3.4, perhaps the best option is:

isinstance(threading.current_thread(), threading._MainThread)

How do I tell if my current thread is the UI thread?

I have found the solution...

CoreDispatcher.HasThreadAccess returns a bool indicating if you are on the UI thread or not.

How to find out if we are running in main thread?

There is no such thing as main thread. There is a thread which was launched first, but all threads are first-class citizens. By tinkering with linker flags, I can easily create a program where the thread executing main() would not be the the thread launched first.

Rethink your design.

How to tell if a thread is the main thread in C#

You could do it like this:

// Do this when you start your application
static int mainThreadId;

// In Main method:
mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;

// If called in the non main thread, will return false;
public static bool IsMainThread
{
get { return System.Threading.Thread.CurrentThread.ManagedThreadId == mainThreadId; }
}

EDIT I realized you could do it with reflection too, here is a snippet for that:

public static void CheckForMainThread()
{
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA &&
!Thread.CurrentThread.IsBackground && !Thread.CurrentThread.IsThreadPoolThread && Thread.CurrentThread.IsAlive)
{
MethodInfo correctEntryMethod = Assembly.GetEntryAssembly().EntryPoint;
StackTrace trace = new StackTrace();
StackFrame[] frames = trace.GetFrames();
for (int i = frames.Length - 1; i >= 0; i--)
{
MethodBase method = frames[i].GetMethod();
if (correctEntryMethod == method)
{
return;
}
}
}

// throw exception, the current thread is not the main thread...
}


Related Topics



Leave a reply



Submit