Re: GTK Progress bar: assistance pls.



Quoting vijayasarathy setsindia net:

        //Call progress_timeout() every 1 second.
    g_timeout_add (100, progress_timeout, pdata);

Ok, you want glib to call progress_timeout() in 100msecs. Glib saves the data somewhere internally and then returns. There is no second thread that would call the function when the timeout times out. glib expects someone to inform it that some time elapsed and that it can call the functions. But no worry, gtk does that for you, the gtk main loop is essentially an endless loop that waits for events, lets glib process the timeout functions etc.

Now you enter a long loop, you don't give gtk control until you finish. That means gtk can't process any events, redraw the widgets, or even let glib process the timeout functions until you return from that callback!

    for(i=0;i<range;i++)
        {
                //Do work w1.
                //g_print("(%d)",i);
                while(j<100000)//To kill time, keep counting until a lakh
                        j++;
                j = 0;//Make j 0 for the next loop.

                //Update fraction
                pdata->fraction = (gdouble)i/(gdouble)(range);

                g_print("(%f)",pdata->fraction);

                /*MYSTERIOUS CODE [FOR ME] -- WORKS !! :-)*/

Oh, but wait. The following while() construct means: gtk, process all pending events. What it does is it lets gtk process input events (mouse movement etc), redraw widgets, and let glib process the timeout function. That is good because now your progress_timeout function can be called, and gtk gets the chance to redraw the progressbar widget and all is fine.

But what you did is not optimal. Why adding the timeout? If you call gtk_progress_bar_set_fraction() right here before the gtk loop it will work just as well! What this code will do is: you tell the progress bar to display a different fraction. The widget will note that and will wait for the next chance to redraw itself. Then, while gtk processes all events, it will note that the progress bar wants to be redrawn, and lets it redraw itself.

So, remove the whole timeout stuff, and just add this call:
                gtk_progress_bar_set_fraction(...)
                while (gtk_events_pending ())
                {
                        g_print("[.]");
                        gtk_main_iteration ();
                }


        }//End for i = start_count to end_count

}


tom




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