Is MainLoop.add can only run in MainThread?

is MainLoop.add can only run in MainThread? or support other threads?

The documentation state:

Add a pending event to be run into the main loop.

it cannot be used for other threads.

If you use the thead api (neko/cpp/cs/hl/java) then there is a function to send messages between threads.

so ,May be Timer.hx much add MainLoop.runInMainThread

public function new(time_ms:Int) {
		#if flash
		var me = this;
		id = untyped __global__["flash.utils.setInterval"](function() {;
		}, time_ms);
		#elseif js
		var me = this;
		id = untyped setInterval(function(), time_ms);
		#elseif java
		timer = new java.util.Timer();
		timer.scheduleAtFixedRate(task = new TimerTask(this), haxe.Int64.ofInt(time_ms), haxe.Int64.ofInt(time_ms));
		MainLoop.runInMainThread(function() {
			var dt = time_ms / 1000;
			event = MainLoop.add(function() {
				@:privateAccess event.nextRun += dt;

That code only use the MainLoop api, not sure what you mean?

I suggest you use the thread api, in haxe 4 you have a global api: sys.thread.Thread - Haxe development API
and in haxe 3 you have to use the target specific api: cs.system.threading.Thread - Haxe 4.2.1 API cpp.vm.Thread - Haxe 4.2.1 API neko.vm.Thread - Haxe 4.2.1 API java.vm.Thread - Haxe 4.2.1 API


Timer will be blocked when Use other Thread .

for example

    //and create timer here will be blocked,and not work.

Yes it seems that you can’t use Timer inside a thread,
but that wouldn’t make sense to have an async callback/interrupt inside a worker thread anyway.

What are you trying to do?

Alternatively you could use the timer in the main thread and use it to send a message to the thread, which could receive/process it when it could.