# Is MainLoop.add can only run in MainThread?

**URL:** https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990
**Category:** Haxe
**Created:** [August 27, 2019, 8:31am UTC](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990 "2019-08-27T08:31:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Zhan](https://community.haxe.org/user_avatar/community.haxe.org/zhan/32/468_2.png) [@Zhan](https://community.haxe.org/u/Zhan)
#### Post date: [August 27, 2019, 8:31am UTC](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990/1 "2019-08-27T08:31:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [August 27, 2019, 8:37am UTC](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990/2 "2019-08-27T08:37:51Z")

</div>

The [documentation](https://api.haxe.org/haxe/MainLoop.html#add) 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.

---

<div class="post-metadata">

### Author: ![Zhan](https://community.haxe.org/user_avatar/community.haxe.org/zhan/32/468_2.png) [@Zhan](https://community.haxe.org/u/Zhan)
#### Post date: [August 27, 2019, 8:39am UTC](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990/3 "2019-08-27T08:39:11Z")

</div>

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() {
			me.run();
		}, time_ms);
		#elseif js
		var me = this;
		id = untyped setInterval(function() me.run(), 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));
		#else
		MainLoop.runInMainThread(function() {
			var dt = time_ms / 1000;
			event = MainLoop.add(function() {
				@:privateAccess event.nextRun += dt;
				run();
			});
			event.delay(dt);
		});
		#end
	}
```

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [August 27, 2019, 8:47am UTC](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990/4 "2019-08-27T08:47:49Z")

</div>

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](https://api.haxe.org/v/development/sys/thread/Thread.html)  
and in haxe 3 you have to use the target specific api: [cs.system.threading.Thread - Haxe 4.2.1 API](https://api.haxe.org/cs/system/threading/Thread.html) [cpp.vm.Thread - Haxe 4.2.1 API](https://api.haxe.org/cpp/vm/Thread.html) [neko.vm.Thread - Haxe 4.2.1 API](https://api.haxe.org/neko/vm/Thread.html) [java.vm.Thread - Haxe 4.2.1 API](https://api.haxe.org/java/vm/Thread.html)

---

<div class="post-metadata">

### Author: ![Zhan](https://community.haxe.org/user_avatar/community.haxe.org/zhan/32/468_2.png) [@Zhan](https://community.haxe.org/u/Zhan)
#### Post date: [August 27, 2019, 8:50am UTC](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990/5 "2019-08-27T08:50:32Z")

</div>

@ibilon

Timer will be blocked when Use other Thread .

for example

```
MainLoop.addThread(function(){
    Sys.sleep(1);
    //and create timer here will be blocked,and not work.
});
```

---

<div class="post-metadata">

### Author: ![ibilon](https://community.haxe.org/user_avatar/community.haxe.org/ibilon/32/13_2.png) [@ibilon](https://community.haxe.org/u/ibilon)
#### Post date: [August 28, 2019, 9:19am UTC](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990/6 "2019-08-28T09:19:38Z")

</div>

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.
