treads.



  Hi all,

I want a progress bar that grow up while the program is reading a file, but
during a getc or a loop the gtk_main seems not to handle other things but
the loop or the kernel call. I wonder if I can solve this using treads and
where I can find a tutorial on glib-gtk multi treading.

        Thanks in advance.


Here is my sample program that don't work.

#include "gtk/gtk.h"
#include "stdio.h"


gint progress_timeout( gpointer data )
{
    gfloat new_val;
    GtkAdjustment *adj;
 
    new_val = gtk_progress_get_value( GTK_PROGRESS(data) );
    
    gtk_progress_set_value (GTK_PROGRESS (data), new_val+1);
    return(TRUE);
}


GtkWidget* createdialog( gchar *message )
{
    GtkAdjustment *adj;
    GtkWidget *dialog;
    GtkWidget *label;
    GtkWidget *progressbar;
    guint timer;

    
    dialog=gtk_dialog_new();
    gtk_widget_set_usize (dialog, 300, 100);
    label = gtk_label_new ("Reading acquisition file...");
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), label, TRUE,
TRUE, 0);
    adj = (GtkAdjustment *) gtk_adjustment_new (0, 1, 100, 0, 0, 0);
    progressbar = gtk_progress_bar_new_with_adjustment (adj);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area),
                        progressbar, TRUE, TRUE, 0);

    timer = gtk_timeout_add (50, progress_timeout, progressbar);

    gtk_widget_show(progressbar);    
    gtk_widget_show(label);
    gtk_widget_show(dialog);
    
    return(dialog);
}


void file_ok_sel( GtkWidget *w, GtkFileSelection *fs )
{
        
    GtkWidget *dialog;
    gchar *filename;
    gchar temp;
    gint i;
    FILE *fd;

    dialog = createdialog("Reading file...");
    filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
    if ( (fd = fopen(filename,"r+b"))==NULL ) {perror("fopen"); exit(2);}
    while( !feof(fd) )
        {
        temp = getc(fd);
        for(i=0;i<500;i++); /* delay */
        }

    gtk_widget_destroy(GTK_WIDGET(dialog));

    return;
}


int main( int argc, char *argv[] )
{
    GtkWidget *filew;
    
    gtk_init (&argc, &argv);
    filew = gtk_file_selection_new ("File selection");
    gtk_signal_connect (GTK_OBJECT (filew), "destroy",
                        (GtkSignalFunc) gtk_widget_destroy, &filew);

    gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),
                        "clicked", (GtkSignalFunc) file_ok_sel, filew );
    gtk_signal_connect_object (GTK_OBJECT
(GTK_FILE_SELECTION(filew)->cancel_button),
                                "clicked", (GtkSignalFunc)
gtk_widget_destroy,
                                GTK_OBJECT (filew));
    
    gtk_file_selection_set_filename (GTK_FILE_SELECTION(filew), "*.dtx");
    gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(filew));
    gtk_widget_show(filew);
    
    gtk_main();

    return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]