2018-12-01 00:02:18 +01:00
|
|
|
package jp.juggler.util
|
|
|
|
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
// threading
|
|
|
|
|
|
|
|
val isMainThread : Boolean get() = Looper.getMainLooper().thread === Thread.currentThread()
|
|
|
|
|
2020-12-17 04:17:48 +01:00
|
|
|
// メインスレッドから呼び出された場合、それがsynchronizedの中だと GlobalScope.launch を使うのは良くない
|
2018-12-01 00:02:18 +01:00
|
|
|
fun runOnMainLooper(proc : () -> Unit) {
|
|
|
|
val looper = Looper.getMainLooper()
|
|
|
|
if(looper.thread === Thread.currentThread()) {
|
|
|
|
proc()
|
|
|
|
} else {
|
|
|
|
Handler(looper).post { proc() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun runOnMainLooperDelayed(delayMs : Long, proc : () -> Unit) {
|
2020-12-17 04:17:48 +01:00
|
|
|
Handler(Looper.getMainLooper()).postDelayed({ proc() }, delayMs)
|
2018-12-01 00:02:18 +01:00
|
|
|
}
|