TThread.Synchronize;
Вызывать из кода обработчика таймера
Чтобы понять, как это происходит, нужно почитать о том, как работает UI Thread (например тут). В этой статье, как раз рассказывает, что такое лупер и как организуется "якобы многозадачность" (отрисовка, обработка событий и тд) в рамках одного треда.
Синхронизация не передает контекст. В данном случае контекст привязан к потоку. Он является частью потока. Поэтому по сути, когда вы синхронизируете (по сути просто вызываете ваш код из лупера, то по сути вы уже работаете в контексте :-)
Если заглянуть в исходники андроида:
public final class Looper {
// Вырезан код
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
}
}
///------------------------------
}
То есть по сути это просто бесконечный цикл, в котором из очереди вытаскиваются задания и выполняются по очереди. Как только возникает событие, оно просто складывается в очередь, и лупер в UI треде его просто обрабатывает. Когда вы вызываете TThread.Synchronize, то в конечном итоге ваш код завернется в сообщение, которое будет вызвано из этого куска кода