c - GtkStatusBar gets stuck at a /random/ position and wants me to mousehover button -
i keep simple
i have function inside_thread
runs while
loop , calls function update_progressbar
updates gtkstatusbar
.
i calling inside_thread
in thread using g_thread_new("processing", gthreadfunc, null);
callback function on_starter_clicked
.
it tends job, works fast , updates status bar according progress of while
loop.
the problem (at unspecified place) progress bar gets stuck , gets unstuck if mouseenter or mouseleave button in application. strange behavior must admit
what wrong?
no, can't update gtkprogressbar thread. there 2 solutions problem, , both involve gdk_threads_idle_add()
:
1. wait idle callback
in case,your thread schedule progress bar update gdk_threads_add_idle()
, wait finish. don't know best way glib is, there's can (gmutex?). idea this:
gboolean updateprogressbar(gpointer data) { gtk_progress_bar_set_fraction(progressbar, value); tellotherthreadtocontinue(); return g_source_remove; // == false } gpointer otherthread(gpointer data) { while (condition) { dostep(); gdk_threads_idle_add(updateprogressbar, null); waitforprogressbartobeupdated(); } return (gpointer) 0; }
2. forego thread entirely , loop idle callback
if idle callback returns g_source_continue
(== true
), gtk+ schedule function again next idle period. can take advantage of rewrite while
loop idle callback:
gboolean loop(gpointer data) { if (condition) return g_source_remove; dostep(); gtk_progress_bar_set_fraction(progressbar, value); return g_source_continue; } // in code, start loop going gdk_threads_idle_add(loop, null);
regardless of choose, can take advantage of data
parameter both idle callback , thread pass data functions need around. careful not pass pointer local variable idle callback, might run after function returns (and variable goes out of scope/ceases exist).
note regardless of do, you'll waiting progress bar update (and in latter case, redraw). if things become slow, may want updating progress bar in chunks. this, however, depends entirely on how many steps have.
Comments
Post a Comment