[Mono-list] [Gtk-sharp-list] Thread-safe GUI update

Chris Seaton chris at chrisseaton.com
Thu Mar 22 16:47:41 EDT 2007


You want to use a semaphore. After AppendAvalues you signal. In place  
of Thread.Sleep you wait on that signal. Something vaguely like

Semaphore semaphore = new Semaphore(0, 1);

while (Messages.Count > 0) {
	msg = (Message) Messages.Dequeue();
	send(msg);

          Gtk.Application.Invoke(delegate {
		treeStore.AppendAvalues(msg.To, msg.Text);
		semaphore.Release(1);
	});

	semaphore.WaitOne();
}

Chris Seaton

On 22 Mar 2007, at 20:31, Leon Stringer wrote:

> Hi,
>
> I'm updating a list (TreeView with TreeStore) from a thread when
> processing some items, something like:
>
> while (Messages.Count > 0) {
> 	msg = (Message) Messages.Dequeue();
> 	send(msg);
>
>          Gtk.Application.Invoke(delegate {
> 		treeStore.AppendAvalues(msg.To, msg.Text);
> 	});
>
> 	Thread.Sleep(0);
> }
>
> There's another thread adding messages to the message queue.
>
> The problem is that sometimes the Gtk.Application.Invoke() is executed
> after I've dequeued the next message so the wrong item is added to the
> list. I could stick a greater value in Thread.Sleep() but this  
> would be
> a kludge. Is there any thread-safe way to do this?
>
> (Mono 1.1.13.8 for Windows)
>
> Thanks in advance,
>
> Leon...
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list



More information about the Mono-list mailing list