Using timers from within a thread causes NoEventLoopException

Hi @haxiomic!
I was looking up an old answer 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!! :smiley:


(So the code now would look like this:)

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