# 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:** 1
**Showing post:** 3

<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.');
                }
            }
        );
    }
}

```

---

_[View the full topic](https://community.haxe.org/t/using-timers-from-within-a-thread-causes-noeventloopexception/3069)._
