Check if thread is a Xojo thread

I just made a feature request to add a function to know if current thread is a Xojo thread.

feedback://showreport?report_id=47037

I would think the functionality you are asking for already exists. For example you could use the REALObjectIsA, or you define a function like below:

bool ThreadSafe_OwnsThread(REALobject instance, REALobject userThread)
{
	ClassData(ThreadSafeClass, instance, ThreadSafeData, data);
	
	if (data->userThread == userThread) {
		XThread theUser = userThread;
		if (theUser.state() == 0) {
			
			XLOG("ThreadSafe_OwnsThread: thread is running (state = 0) with userThread.lockCount = %d", Lockcount(userThread));
            if (data->useSuspend) {
                XLOG("USING THREAD.SUSPEND", 0);
            } else {
                XLOG("USING SEMAPHORE", 0);
                if (!data->blocker) {
                    data->blocker = REALnewInstanceWithClass(gSemaphore);//(REALGetClassRef("Semaphore"));
                    data->blocker.constructor();
                    XLOG("semaphore installed, lockCount = %d", Lockcount((REALobject)data->blocker));
                    data->blocker.signal();
                    XLOG("initial signal of the semaphore called", 0);
                }
                if(!data->blocker) {
                    XLOG("ThreadSafe_OwnsThread: Semaphore instantiation failed", 0);
                    return false;
                }
                XLOG("returning true from ThreadSafe_OwnsThread", 0);
            }
			return true;
			
		} else if (theUser.state() == 1) {
			// thread is waiting, using its own semaphore, bail out
            REALobject waiting = REALnewInstanceWithClass(gThreadAllreadyWaitingException);//(REALGetClassRef("ThreadAllreadyWaitingException"));
			REALRaiseException(waiting);
			return false;
			
		} else if (theUser.state() == 2) {
			// thread is suspended, so this shouldn't be harmful
			return true;
			
		} else if (theUser.state() == 3) {
			// thread is sleeping, so this shouldn't be harmful
			return true;
			;
		} else if (theUser.state() == 4) {
			// thread is not running, so bail out
            REALobject notRunning = REALnewInstanceWithClass(gThreadNotRunningException);//(REALGetClassRef("ThreadNotRunningException"));
			REALRaiseException(notRunning);
			return false;
		}
	}
	return false;
}

REALobject SharedMethod_GetInstance(REALobject thread, bool usesuspend)
{
	// make sure our class will be associated with a thread
	// actually, this method should be called in a running thread 
	if (!thread) {
		return nil;
	}

    // get our instance if it already exists
	REALobject threadSafeObject = _FindInstanceFromThread(thread);

	if (!threadSafeObject) {
        threadSafeObject = REALnewInstanceWithClass(gThreadSafeGUIProcessor);//(REALGetClassRef("ThreadSafeGUIProcessor"));
		ClassData(ThreadSafeClass, threadSafeObject, ThreadSafeData, data);

        XThread theUser = thread;
        theUser.lock();

		data->userThread = thread;
        data->useSleep = false;
        data->useSuspend = usesuspend;
        
	} else {
        XLOG("in SharedMethod_GetInstance, found a user thread already associated with a ThreadSafeGUIProcessor", 0);
        ClassData(ThreadSafeClass, threadSafeObject, ThreadSafeData, data);
        data->useSleep = false;
        data->useSuspend = usesuspend;
	}
    
  //  ThreadSafe_Threadstate(threadSafeObject); // just a function to check the state of the thread.

	if (ThreadSafe_OwnsThread(threadSafeObject, thread))
		return threadSafeObject;
    
    LOG("in SharedMethod_GetInstance, bailing out, ThreadSafe_OwnsThread is returning false");
	REALUnlockObject(threadSafeObject);
	return nil;
}

With respect to the Mainthread, I would not count to much on having something like that. Suppose Xojo gives you an API for this. But the next thing that could happen is that somewhere a thread is resumed and you have no control over it. In the above creation of a thread by the “Threadsafe” class, you check whether the plugin is owning the thread. Then if you need to run something in the main thread you’ve got to spawn a timer. Don’t rely on Xojo.

How would this help me to know if the current thread is one created by Xojo?