the correct way to deal with gtk_events_pending and gtk_main_iteration
Posted
by
abd alsalam
on Ask Ubuntu
See other posts from Ask Ubuntu
or by abd alsalam
Published on 2014-06-08T20:51:22Z
Indexed on
2014/06/08
21:41 UTC
Read the original article
Hit count: 287
I have program that send files and i want to make a progress bar for it, but that progress bar just updated after the transferring complete,so i putted a gtk_events_pending() and gtk_main_iteration() functions in the sending loop to go back to the gtk main loop to update the progress bar but also it seems to not work here is a
EDIT: the send function is in a separated thread
snippet from my code
float Percent = 0.0 ;
float Interval = 0.0 ;
the sending function
gint SendTheFile ( )
{
char FileBlockBuffer[512];
bzero(FileBlockBuffer, 512);
int FileBlockSize ;
FILE * FilePointer ;
int filesize = 0 ;
FilePointer = fopen(LocalFileName , "r");
struct stat st;
stat(LocalFileName, &st);
filesize = st.st_size;
Interval = (512 / (float)filesize) ;
while((FileBlockSize = fread(FileBlockBuffer,sizeof(char),512,FilePointer))>0)
{
send(SocketDiscriptor , FileBlockBuffer , FileBlockSize,0);
bzero(FileBlockBuffer, 512);
Percent = Percent + Interval ;
if (Percent > 1.0)Percent = 0.0;
while(gtk_events_pending() )
{
gtk_main_iteration();
}
}
update progress bar function
gint UpdateProgressBar(gpointer data)
{
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(data),Percent);
}
updating progress bar in the main function
g_timeout_add(50,(GSourceFunc)UpdateProgressBar,SendFileProgressBar);
© Ask Ubuntu or respective owner