App.currentThread in API 2.1

Xojo 2020r2 tells me that App.currentThread is deprecated. The docs tell me about the replacement Thread.Current

While this property is currently Nil when accessed from the main thread, do NOT rely on this behavior as it may not be the case in future releases.

What is this supposed to tell me? When I’m changing my code I want to at least a tiny bit future-proof my code so that I don’t have to touch it again in the next version.

I’m using App.currentThread in situations where I need to know if I’m in a thread or not. The code doesn’t need any reference to a thread:

if app.CurrentThread <> nil then
  PBMails.SetVisibleThreadSafeMBS isVisible
  STMailInfo.SetVisibleThreadSafeMBS isVisible
else
  PBMails.Visible = isVisible
  STMailInfo.Visible = isVisible
end if

As fas as I see, Thread.Current and Application.CurrentThread both just call REALGetCurrentThread internally in the framework. No difference.

And a note here:
SetVisibleThreadSafeMBS checks whether you call it on main thread. And then it just sets the property directly. Only when you are on a helper thread, the call is dispatched to main thread.
So your whole check is not needed.

Very good. Is this also true for

SetEnabledThreadSafeMBS
CallDelegateOnMainThreadMBS
SetMaximumThreadSafeMBS
SetVisibleThreadSafeMBS
SetTextThreadSafeMBS

?

Yes

Very good. So 80% of problem solved.

However, I’ve got a couple of other locations with code like:

dim currentThread as Thread = App.CurrentThread
if currentThread = nil then
  HandleAction(Nil)
  return
end if

'create the timer
me.ShowDialog = new DelegatingTimer
me.ShowDialog.RunMode = Timer.RunModes.Single
me.ShowDialog.Period = 1
me.ShowDialog.Action = AddressOf me.HandleAction

Here I’m showing a dialog.

Nevertheless, it is useful for code to be able to determine whether it’s running on the main thread or not. That way I can have a method callable anywhere, without the caller having to worry.

That’s my main reason for asking the question now. I haven’t the foggiest what the sentence in the docs is supposed to mean. We need to be able to say if something is in the main thread or not.

2 Likes