Destructor/Disposable Pattern

158 views
Skip to first unread message

Ashiq A.

unread,
Jun 13, 2017, 3:19:53 PM6/13/17
to haxe...@googlegroups.com
Hi,

I'm working on a class that includes a haxe.Timer instance as an internal implementation detail. According to the docs, a running timer could prevent my program from terminating.

How do I guarantee that this terminates/closes, especially if I have to create some sort of "stop" method that the user might forget to call?

I thought about using a destructor or something like C#'s "disposable" interface (the user can call "dispose" or you can wire up the class so that the GC calls it automatically when garbage-collected). But, it seems like I can't write a destructor in Haxe.

How have others worked around this kind of problem?

--Ashiq

Hugh

unread,
Jun 13, 2017, 9:27:19 PM6/13/17
to Haxe
Are you doing a command-line or gui app?
For a gui app, closing the window or calling "exit" will usually terminates whether there are timers pending or not - but this depends on the framework you are using.

For command line, if the user stets a timer for 1 hour, and then the "static main" function completes, do you expect the program to terminate or wait around for an hour?  I don't thing finalizers are really going to help here.

Hugh

der Raab

unread,
Jun 15, 2017, 3:54:21 AM6/15/17
to Haxe
I don't know anything about native targets, but if there's a chance that a long running timer would block the system, why not building a little util with an api like Util.callMethodInMilliseconds( method, milliseconds ) and internally just create an timestamp and an internal timer which checks every 100 ms if the current timestamp is greater or equal to the expected time and then just execute your method and stop the internal timer?

So the idea is to avoid timers with long intervals times? :) Just an idea...

Ashiq A.

unread,
Jun 15, 2017, 4:46:38 AM6/15/17
to haxe...@googlegroups.com
I'm creating a library that I'm integrating with HaxeFlixel. I'm pretty sure this doesn't matter for flash/html5 (application closes when the window closes) and mobile (ditto) targets, but I'm not sure about the C++/desktop targets.

I don't know if this is an issue or not. For now, I'm looking into "exit" hooks for HaxeFlixel/Lime that allow me to stop my timer when the app terminates. There's one that works well for mobile, but it doesn't seem to work on desktop (tested with Neko).

Thanks,

--Ashiq


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Hugh

unread,
Jun 16, 2017, 12:20:41 AM6/16/17
to Haxe
Nme does not stop the app from exiting when there are timers pending.  I'm pretty sure Lime does the same thing - the blocking thing is more about a command line when there is no good way for the user to signal, "ok, I'm  done now".

Ashiq A.

unread,
Jun 16, 2017, 4:55:31 AM6/16/17
to haxe...@googlegroups.com
So I don't need to worry about this? Essentially, with a non-command-line app (or  rather, a lime/NME app), it's guaranteed to terminate every time?

Hugh

unread,
Jun 19, 2017, 12:35:24 AM6/19/17
to Haxe
Nme app for sure.  Lime - a little test will tell you.

Hugh
Reply all
Reply to author
Forward
0 new messages