Re: How to force the refresh of a GTKTreeView in a multithreaded app?

On Thu, Jun 21, 2007 at 06:30:03PM -0400, Álvaro Palma wrote:
> I've developed a very simple application in GTK2. This application 
> receives events from a socket and based on them, updates the values in a 
> GTKTreeView.
> The problem that arises is that the items displayed in the TreeView are 
> not updated until I move the mouse.

Then the tree model does not emit "row-changed" (or other
appropriate signals) when it should, i.e. when the rows
change.  The fix is to emit the signals when they should be
emitted.  All views showing the model can then update

Since accessing the GUI from multiple threads is not
portable, I suggest to use g_idle_add() to queue the signal
emission, as is usually recommended.  Actually, putting the
complete model update there greatly simplifies locking
(often to no explicit locking).

> SocketEvents()
> {
>    ...
>    while(1){
>      <receive an event from a socket>
>      ....
>      gdk_threads_enter();

Depending on the application code this may or may not be
sufficient locking.

>      gtk_list_store_set(....);
>      gtk_widget_queue_draw(GTK_WIDGET(gtkTreeView));

MVC works this way:  You update the model, all views notice
it and update themselves.  Explicitly calling
gtk_widget_queue_draw() on the views is, err, suboptimal.

>      while (gtk_events_pending ())
>        gtk_main_iteration ();

Invoking gtk_main_iteration() in other threads than that
running gtk_main() will have ,interesting` consequences...

> (MyAPP:12142): GLib-WARNING **: g_main_context_prepare(): main loop 
> already active in another thread
> Xlib: unexpected async reply (sequence 0x1ee7)!

> GLib-ERROR **: file gmain.c: line 1906 (g_main_dispatch): assertion 
> failed: (source)
> aborting...

...such as these.


    while (gtk_events_pending())

should be generally avoided even in single-threaded programs,
although it is sometimes employed.  It makes no sense to do
this in multi-threaded programs -- just keep the thread
running gtk_main() free of heavy work and do not make it
block too much, and it will happily update the UI.

> Can anybody give me a clue about how do I have to update the list store
> to be able to see my changes reflected in the screen right away?

Please search the mailing list archives.  The proper use of
threads is discussed very often.



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