# Using timers from within a thread causes NoEventLoopException

**URL:** https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069
**Category:** Haxe
**Created:** [May 29, 2021, 7:25am UTC](https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069 "2021-05-29T07:25:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![AlienBuchner](https://community.haxe.org/user_avatar/community.haxe.org/alienbuchner/32/1387_2.png) [@AlienBuchner](https://community.haxe.org/u/AlienBuchner)
#### Post date: [May 29, 2021, 7:25am UTC](https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069/1 "2021-05-29T07:25:06Z")

</div>

Hi! Try this sample code snippet.  
It seems impossible to use timers within a thread.

```haxe
class TimerStartedWithinThread {
    public static function main() {
        trace("Entered main method.");

        // regular timer
        var t = new haxe.Timer( 1000*2 );
        t.run = function() {
            trace("Hello from main timer.");
        }

        // an "outthreaded" timer that causes a `NoEventLoopException`
        /*
        sys.thread.Thread.create(
            function() {
                var t = new haxe.Timer( 1000*2 );
                t.run = function() {
                    trace('Hello from sub timer.');
                }
            }
        );*/
    }
}

```

Is there a possibility to use timers startet from another thread?  
Thank you. 🙂

---

<div class="post-metadata">

### Author: ![haxiomic](https://community.haxe.org/user_avatar/community.haxe.org/haxiomic/32/75_2.png) [@haxiomic](https://community.haxe.org/u/haxiomic)
#### Post date: [May 29, 2021, 9:45am UTC](https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069/2 "2021-05-29T09:45:49Z")

</div>

Hey @AlienBuchner, you want to use Thread.createWithEventLoop(), all events will be executed within that thread. You could also use haxe.MainLoop.runInMainThread() so your events will run in the main thread (and then your thread doesn’t need its own event loop)

(Maybe we can improve error reporting here!)

---

<div class="post-metadata">

### Author: ![AlienBuchner](https://community.haxe.org/user_avatar/community.haxe.org/alienbuchner/32/1387_2.png) [@AlienBuchner](https://community.haxe.org/u/AlienBuchner)
#### Post date: [May 29, 2021, 12:06pm UTC](https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069/3 "2021-05-29T12:06:26Z")

</div>

Hi @haxiomic!  
I was looking up an [old answer](https://community.haxe.org/t/is-mainloop-add-can-only-run-in-mainthread/1990/6) in the forum and reconsidered if this feature is really required in my case and so I rearranged my code structure. But it’s very nice to know these two options and they seem to work fine! So thanks a lot!! 😃

* * *

(So the code now would look like this:)

```haxe
class TimerStartedWithinThread {
    public static function main() {
        trace("Entered main method.");

        // regular timer
        var t = new haxe.Timer( 1000*2 );
        t.run = function() {
            trace("Hello from main timer.");
        }
        
        // option 1
        sys.thread.Thread.createWithEventLoop( // <---------- this changed
            function() {
                var t = new haxe.Timer( 1000*2 );
                t.run = function() {
                    trace('Sub timer: I have my very own event loop!');
                }
            }
        );

        // option 2
        haxe.MainLoop.runInMainThread( // <---------- this changed
            function() {
                var t = new haxe.Timer( 1000*2 );
                t.run = function() {
                    trace('Sub timer: I just use the default main event loop.');
                }
            }
        );
    }
}

```

---

<div class="post-metadata">

### Author: ![d0oo0p](https://community.haxe.org/user_avatar/community.haxe.org/d0oo0p/32/170_2.png) [@d0oo0p](https://community.haxe.org/u/d0oo0p)
#### Post date: [May 29, 2021, 6:50pm UTC](https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069/4 "2021-05-29T18:50:16Z")

</div>

It seems that starting a new timer from a delay or timer callback also will crash, atleast with hxcpp. Im guessing this is related, but code working with 4.1.2 is now broken in 4.2… So should that count as a bug?.. 🤔

---

<div class="post-metadata">

### Author: ![haxiomic](https://community.haxe.org/user_avatar/community.haxe.org/haxiomic/32/75_2.png) [@haxiomic](https://community.haxe.org/u/haxiomic)
#### Post date: [May 30, 2021, 8:39pm UTC](https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069/5 "2021-05-30T20:39:23Z")

</div>

Could you post a repro example @d0oo0p, this definitely shouldn’t crash!
