Clarification about gtk_main() and running simultaneous non-gtk functions

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Clarification about gtk_main() and running simultaneous non-gtk functions

Michal Porzuczek
Correct me if I'm wrong but the gtk_main() acts like an infinite while
loop that waits for callbacks to the widgets that have been created
before gtk_main() was called.

My question is, except for the g_timer function which seems to just be
for measuring proccesses inside actual callbacks, is it possible to
have timers running simultaneously with gtk_main() without calling a
separate program? That is, would it be possible to create a seperate
while(1) that say checks for the value of some variable while
gtk_main() is already running?

Thanks

Michal
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Clarification about gtk_main() and running simultaneous non-gtk functions

Tristan Van Berkom
Michal Porzuczek wrote:
> Correct me if I'm wrong but the gtk_main() acts like an infinite while
> loop that waits for callbacks to the widgets that have been created
> before gtk_main() was called.

gtk_main will run a GMainLoop:
http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html

The main loop will process "event sources" (IO events and timeout events).

X server events get interpreted by GDK and sent to the appropriate GTK+
objects for processing as "GSignals" (such as "button-press-event");
sometimes other signals are emitted as a result (such as "clicked"
on a button which recieved a "button-press-event" followed by a
"button-release-event").

Note that signals are emitted synchronously and are not deffered to the
main loop.

> My question is, except for the g_timer function which seems to just be
> for measuring proccesses inside actual callbacks, is it possible to
> have timers running simultaneously with gtk_main() without calling a
> separate program? That is, would it be possible to create a seperate
> while(1) that say checks for the value of some variable while
> gtk_main() is already running?

     You probably want to register a timeout GSource into your mainloop
using g_timeout_add (), so that gtk_main () (i.e. the GMainLoop)
will wakeup at the requested time and call your callback.

If you run a "while (1)" in your process; then you are hijacking
the main loop and your interface wont respond (not to mention that
all your event sources, such as IO watches and timeouts wont
be run either).

If you must see "while (1)" in your code; you'll have to do it in
another process or another thread.

Cheers,
                                -Tristan
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Clarification about gtk_main() and running simultaneous non-gtk functions

Paul Pogonyshev
In reply to this post by Michal Porzuczek
Michal Porzuczek wrote:
> Correct me if I'm wrong but the gtk_main() acts like an infinite while
> loop that waits for callbacks to the widgets that have been created
> before gtk_main() was called.

It is true to some extent.  It is an infinite, but terminatable loop that
waits for events (most notably, from X Window System or whatever backend
is used.)  However, it is _not_ limited to that kind of events.

> My question is, except for the g_timer function which seems to just be
> for measuring proccesses inside actual callbacks, is it possible to
> have timers running simultaneously with gtk_main() without calling a
> separate program?  That is, would it be possible to create a seperate
> while(1) that say checks for the value of some variable while
> gtk_main() is already running?

Yes, if you run it in a separate thread.  However, there is a better
(for most purposes) option.

Define your own GSource.  It can check anything you wish and run any
callbacks when events it catches arrive.  Your source will be used from
within GTK+ (actually, GLib's) main loop, but if you need it to be really
responsive, you can elevate your event source priority.  But even the
default priority must be OK for most purposes.

Paul

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list