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

Michael Hutchinson m.j.hutchinson at gmail.com
Sun Mar 25 16:46:11 EDT 2007


On 3/25/07, Leon Stringer <leon.stringer at ntlworld.com> wrote:
> William Garrison wrote:
> > Wouldn't using a regular delegate eliminate this issue?  It would copy
> > msg onto the stack for the call so the value wouldn't be overwritten
> > during the next loop.  (I'm not sure if this code is quite right, but
> > you get the idea)
> >
> > while (Messages.Count > 0) {
> >     msg = (Message) Messages.Dequeue();
> >     send(msg);
> >     Gtk.Application.Invoke(AppendAValue,msg);
> >     Thread.Sleep(0);
> > }
> >
> > private void AppendAValue(Message msg)
> > {
> >     treeStore.AppendAvlues(msg.To, msg.Text);
> > }
>
> I realise you've done this example off the top of your head but do you
> know of any working examples?
>
> As far as I can see I have to do something like:
>
>     Gtk.Application.Invoke(this, msg, AppendAValue);
>
> but it won't compile because msg isn't the right type (EventArgs).

Remember I mentioned Gtk.Application.Invoke (object sender,
System.EventArgs args, System.EventHandler d) could be a solution?

while (Messages.Count > 0) {
    msg = (Message) Messages.Dequeue();
    send(msg);
    Gtk.Application.Invoke(this, new MsgEventArgs(msg), new delegate
            (object sender, MsgEventArgs args) {
                treeStore.AppendAvalues(args.Message.To, args.Message.Text);
            }
    );
    Thread.Sleep(0);
}

class MsgEventArgs : EventArgs
{
    public Message Message;
    public MsgEventArgs (Message msg)
    {
        Message = msg;
    }
}

EventArgs-derived classes are the standard C# pattern for arguments to
event handlers. Of course you can add convenience constructors, use
properties for accessors etc.

-- 
Michael Hutchinson
http://mjhutchinson.com


More information about the Gtk-sharp-list mailing list