Thread overview
Re: event based timer
Jul 20, 2011
Sean Kelly
Jul 20, 2011
maarten van damme
Jul 20, 2011
Sean Kelly
Jul 20, 2011
maarten van damme
Jul 20, 2011
Chris Molozian
Jul 20, 2011
Andrej Mitrovic
Jul 20, 2011
maarten van damme
July 20, 2011
On Jul 19, 2011, at 3:19 AM, maarten van damme wrote:

> Hi everyone,
> for getting to know d a little bit I'm writing a simple pingpong game using gtk.
> for it to work I need to be able to do something every 0.1 seconds so I was wondering if there was some kind of timer in the phobos library.
> I've looked everywhere but couldn't find one. Is it missing and do i have to write my own or have I overlooked it?

void loop() {
    while( true ) {
        doWhatever();
        Thread.sleep(dur!"msecs"(100));
    }
}

spawn(&loop);


Or you could have the spawned thread send a message every 100 milliseconds, etc.
July 20, 2011
The problem with Sean Kelly's and the message sending approach is that when using gtk I have to call Main.run(); and it pauses then untill the windows are all closed. when I place a loop before Main.run() I never get a window so If I want to use some kind of timer using threads I need to use shared? (correct me if I'm wrong)

The glib.Timeout works like a charm, Thank you chris

2011/7/20 Sean Kelly <sean@invisibleduck.org>

> On Jul 19, 2011, at 3:19 AM, maarten van damme wrote:
>
> > Hi everyone,
> > for getting to know d a little bit I'm writing a simple pingpong game
> using gtk.
> > for it to work I need to be able to do something every 0.1 seconds so I
> was wondering if there was some kind of timer in the phobos library.
> > I've looked everywhere but couldn't find one. Is it missing and do i have
> to write my own or have I overlooked it?
>
> void loop() {
>    while( true ) {
>        doWhatever();
>        Thread.sleep(dur!"msecs"(100));
>    }
> }
>
> spawn(&loop);
>
>
> Or you could have the spawned thread send a message every 100 milliseconds, etc.


July 20, 2011
Is there a timer function built into gtk?  Or can you have a separate thread trigger a gtk event on a timer?

On Jul 20, 2011, at 5:00 AM, maarten van damme wrote:

> The problem with Sean Kelly's and the message sending approach is that when using gtk I have to call Main.run(); and it pauses then untill the windows are all closed. when I place a loop before Main.run() I never get a window so If I want to use some kind of timer using threads I need to use shared? (correct me if I'm wrong)
> 
> The glib.Timeout works like a charm, Thank you chris
> 
> 2011/7/20 Sean Kelly <sean@invisibleduck.org>
> On Jul 19, 2011, at 3:19 AM, maarten van damme wrote:
> 
> > Hi everyone,
> > for getting to know d a little bit I'm writing a simple pingpong game using gtk.
> > for it to work I need to be able to do something every 0.1 seconds so I was wondering if there was some kind of timer in the phobos library.
> > I've looked everywhere but couldn't find one. Is it missing and do i have to write my own or have I overlooked it?
> 
> void loop() {
>    while( true ) {
>        doWhatever();
>        Thread.sleep(dur!"msecs"(100));
>    }
> }
> 
> spawn(&loop);
> 
> 
> Or you could have the spawned thread send a message every 100 milliseconds, etc.
> 

July 20, 2011
It turned out there was a Timer in glib and glib is a part of gtk(I think).
When you use gtkd you have acces to glib and that worked but it takes 50
procent of my cpu. (this was actually pretty strange. It worked fast and
good untill I rebooted. Now it runs prety slow)
and I don't think you can trigger a gtk event on a timer.

2011/7/20 Sean Kelly <sean@invisibleduck.org>

> Is there a timer function built into gtk?  Or can you have a separate thread trigger a gtk event on a timer?
>
> On Jul 20, 2011, at 5:00 AM, maarten van damme wrote:
>
> > The problem with Sean Kelly's and the message sending approach is that
> when using gtk I have to call Main.run(); and it pauses then untill the
> windows are all closed. when I place a loop before Main.run() I never get a
> window so If I want to use some kind of timer using threads I need to use
> shared? (correct me if I'm wrong)
> >
> > The glib.Timeout works like a charm, Thank you chris
> >
> > 2011/7/20 Sean Kelly <sean@invisibleduck.org>
> > On Jul 19, 2011, at 3:19 AM, maarten van damme wrote:
> >
> > > Hi everyone,
> > > for getting to know d a little bit I'm writing a simple pingpong game
> using gtk.
> > > for it to work I need to be able to do something every 0.1 seconds so I
> was wondering if there was some kind of timer in the phobos library.
> > > I've looked everywhere but couldn't find one. Is it missing and do i
> have to write my own or have I overlooked it?
> >
> > void loop() {
> >    while( true ) {
> >        doWhatever();
> >        Thread.sleep(dur!"msecs"(100));
> >    }
> > }
> >
> > spawn(&loop);
> >
> >
> > Or you could have the spawned thread send a message every 100
> milliseconds, etc.
> >
>
>


July 20, 2011
Yes, Gtk is built using GLib and so depends on it. Here's some example code I'm using in a Vala project  (sorry) that uses GLib.Timeout:

public class TimeoutEntry : Granite.Entry, Gtk.Buildable {
private static const uint DEFAULT_TIMEOUT = 300; // in ms
/**
* The timeout length in milliseconds before {@see #timed_out} signal
* is called. The value defaults to 300.
*/
public uint timeout { get; set; default = DEFAULT_TIMEOUT; }
/**
* A callback fired after {@see timeout} length of time has expired in the
* non-empty entry field.
*/
public signal void timed_out();
private bool timeout_changed = false;
construct {
// add signals and callbacks
notify["timeout"].connect(() => { timeout_changed = false; });
realize.connect(() => { GLib.Timeout.add(timeout, emit_timed_out); });
changed.connect(() => { timeout_changed = true; }); // reset timeout
}
private bool emit_timed_out() {
if (timeout_changed) {
GLib.Timeout.add(timeout, emit_timed_out);
return (timeout_changed = false);
}
if (text != "") { timed_out(); }
return !timeout_changed;
}
}


Here I'm calling the timed_out signal which will execute an associated callback. I'm not sure what you mean by "trigger a gtk event on a timer", if I understand it correctly this should be helpful gobject.Signals.emitv <http://gtkd.mikewey.eu/src/gobject/Signals.html> and have a look here: http://stackoverflow.com/questions/1557025/create-and-emit-gtk-signal

Hope this helps,

Chris

PS: the problems you're encountering seem less and less like problems with D (unless they're compiler bugs)... and more like issues with Gtk/GLib. The GTK IRC channel can be very (very) quiet but it might help.


On 07/20/11 14:34, maarten van damme wrote:
> It turned out there was a Timer in glib and glib is a part of gtk(I
> think). When you use gtkd you have acces to glib and that worked but
> it takes 50 procent of my cpu. (this was actually pretty strange. It
> worked fast and good untill I rebooted. Now it runs prety slow)
> and I don't think you can trigger a gtk event on a timer.
>
> 2011/7/20 Sean Kelly <sean@invisibleduck.org <mailto:sean@invisibleduck.org>>
>
>     Is there a timer function built into gtk?  Or can you have a
>     separate thread trigger a gtk event on a timer?
>
>     On Jul 20, 2011, at 5:00 AM, maarten van damme wrote:
>
>     > The problem with Sean Kelly's and the message sending approach
>     is that when using gtk I have to call Main.run(); and it pauses
>     then untill the windows are all closed. when I place a loop before
>     Main.run() I never get a window so If I want to use some kind of
>     timer using threads I need to use shared? (correct me if I'm wrong)
>     >
>     > The glib.Timeout works like a charm, Thank you chris
>     >
>     > 2011/7/20 Sean Kelly <sean@invisibleduck.org
>     <mailto:sean@invisibleduck.org>>
>     > On Jul 19, 2011, at 3:19 AM, maarten van damme wrote:
>     >
>     > > Hi everyone,
>     > > for getting to know d a little bit I'm writing a simple
>     pingpong game using gtk.
>     > > for it to work I need to be able to do something every 0.1
>     seconds so I was wondering if there was some kind of timer in the
>     phobos library.
>     > > I've looked everywhere but couldn't find one. Is it missing
>     and do i have to write my own or have I overlooked it?
>     >
>     > void loop() {
>     >    while( true ) {
>     >        doWhatever();
>     >        Thread.sleep(dur!"msecs"(100));
>     >    }
>     > }
>     >
>     > spawn(&loop);
>     >
>     >
>     > Or you could have the spawned thread send a message every 100
>     milliseconds, etc.
>     >
>
>


July 20, 2011
At least on Windoze it's a simple case of SetTimer(hwnd, ID_TIMER, msecs, NULL);

For callbacks on Windows timeSetEvent() is ok.
July 20, 2011
If someoene feels like playing some good old pong, go here:
http://dl.dropbox.com/u/15024434/Ball.exe
Beeing so slow seemed to be a temporary problem, it works good right now.

I'm really hoping on a good timer for phobos, having to use a tird party library for something like a timer seems kind off ridiculous coming from java :). I've heard the c++ boost library has a timer, maybe someone can look at it and port it to D or write one from scratch?

Other than that thanks for all the help.