GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Sep 18, 2014 5:39 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Main window hanging during callback
PostPosted: Sat Jul 16, 2011 12:04 am 
Offline
Familiar Face

Joined: Sat Jul 09, 2011 8:10 pm
Posts: 23
Hi again

Im a bit stumped here.
I have a progress bar on my main window to show the progress of a file_copy.
When i click the [copy] button the callback is working (ie it copies the file) but
because it takes long (2GB file) the main window goes grey and progress bar doesnt move until
write_file() returns.
when it returns,the progress bar jumps to the full position and the window is not grey anymore.
I tried writing file in small chunks and returning from callback with TRUE,but it doesnt re-enter the callback.
How do i get this to work?

Thanks
Deon


Top
 Profile  
 
 Post subject: Re: Main window hanging during callback
PostPosted: Sat Jul 16, 2011 7:33 am 
Offline
GTK+ Geek

Joined: Sun Apr 03, 2011 11:12 am
Posts: 63
This is a general problem. You have to use Threads for this. Sorry, I can't provide an example in C.


Top
 Profile  
 
 Post subject: Re: Main window hanging during callback
PostPosted: Sat Jul 16, 2011 8:29 am 
Offline
Never Seen the Sunlight

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

Alternative to multi-threaded application, you can do your copying from idle handler in small chunks. If you're interested in this approach, I can provide a simple example.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: Main window hanging during callback
PostPosted: Sat Jul 16, 2011 8:32 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hi,

This is more of an FAQ!

Basically what you have done is block the GUI, so it can not be updated.

There are many different ways to work with this.

First solution is the "quick and dirty way", and not really a way to be recommended unless you have a dialog window that is "modal".

So within your copying of data loop, every so often when you what to up date the display run this section of code.
Code:
/* computation going on... */

  while (gtk_events_pending ())
    gtk_main_iteration ();

/* ...computation continued */


------------

Another way is to break up your data that is to be copied into smaller chunks and use g_idle_add() to add a call-back that does your copying and update of your progress bar. So within the g_idle_add() call-back you copy a small chunk of your data, then update your progress bar, if there is more data to be copied return TRUE (allow the call-back to continue) and if the copy has finished return FALSE (removes the call-back). What you will have to do is some how keep track of how much data you have copied and your position, then opening and closing of files etc..

------------

The final way, as the last poster suggested is to use threads, it can also be the most efficient way of doing this if you have a multi CPU machine. It is also the most error prone, due to most people not really understanding how to use them properly and ending up with some very surprising run time results which will vary from machine to machine, and even from each time the application is run.

Basically what you will need to do is start a thread to do the copying (this is your worker thread). You then have to communicate from your worker thread to your main thread (the one GTK+ is on) when to update your progress bar. There are many ways to do this, the first and easiest is to use g_idle_add(GSourceFunc function, gpointer data) which is called in your worker thread (this is thread safe). You then use the user data to communicate how much data has been copied (probably as a percentage). The call-back will then be called in the main thread and in this you can then use the user data to update your progress bar and then you need to return FALSE to remove the call-back. (more will be added with the worker thread). There are also many other ways of doing, just use your search engine on Inter Process Communication.

Errol.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Main window hanging during callback
PostPosted: Sat Jul 16, 2011 8:41 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Tadej, we posted within minutes! :)

Be my guest if you want to do an example for the original poster, as I shall be away from my computer for some time.

Errol.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Main window hanging during callback
PostPosted: Sat Jul 16, 2011 8:44 am 
Offline
Familiar Face

Joined: Sat Jul 09, 2011 8:10 pm
Posts: 23
Yup . It was after midnight and i was hoping for magic fix.;-)

Wrapped it in a pthread this morning and its working fine.

Thanks
Deon


Top
 Profile  
 
 Post subject: Re: Main window hanging during callback
PostPosted: Sat Jul 16, 2011 8:52 am 
Offline
Familiar Face

Joined: Sat Jul 09, 2011 8:10 pm
Posts: 23
Ha sorry i only read the first response.

Thats what i was looking for.
A way to make Gtk loop through its main to update its controls.

However as i just posted, wrapping it in a thread was my solution.I also had to add a timer to
update the progress bar.

Thanks for the ideas guys.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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