GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Aug 02, 2014 8:35 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Why are widgets initially shown when callback function ends?
PostPosted: Fri Jul 10, 2009 4:01 pm 
Offline

Joined: Fri Jul 10, 2009 3:35 pm
Posts: 1
Hi everyone,

I am new with GTK and in this forum. I am working with GTK 2.10.14 in C with a Scientific Linux OS and these are my first experiences with GUI programming.

I build an application, which consists of a window and several buttons and other widgets. One of the buttons - the "Execute" button - should start a command line application (using the system()-C-function) as well as show a progressbar-widget in the main window. I have written a callback function, which implements both of these features. The problem is, that the progressbar is initially shown, when the console application ends, not when it starts.
Is there any way to force a widget to show up, at the first time it was created? Or is there something I do fundamentally wrong?
I also tried to start the system-function via g_idle_function_full (Glib), but the problem still persists.
I am pretty sure that many applications follow such an approach, so I assume that there must be a simple solution. Can anyone help me with this?
Thanx, ITJ


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 15, 2009 9:08 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums.

This is one of the most common problems newcomers encounter when trying to execute a task that will take some time to finish from within callback handler. Why is this problem? Because callback handlers block main loop that updates the GUI, so no refreshing is done while lengthy task is running.

There are two solutions to this problem: the easy and ugly one; and the hard and right one.

The easy one involves calling this snippet of code whenever one wishes to update GUI and process any pending events:
Code:
while( gtk_events_pending() )
    gtk_main_iteration();
As I said, this is considered an ugly hack that tend to get abused when task in question is executed in some kind of loop.

The right thing to do when you need to execute lengthy task is to either run it in separate thread or spawn another process and monitor it from main application. Glib offers cross-platform infrastructure for both of these methods: GThreads for threading support and spawning support launching external applications.

I hope this introduction helped a bit. For more information on threads, you can check out my blog post here: http://tadeboro.blogspot.com/2009/06/multi-threaded-gtk-applications.html. I'm currently writing post on spawning, so check out my blog in a few days for even more info.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 15, 2009 8:56 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

I managed to get my spawning post done. You can find it here: http://tadeboro.blogspot.com/2009/07/spawning-processes-using-glib.html-

I hope you'll find it useful.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group