469,631 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

Threaded Cross-Class Winform Manipulation

i
Hi,

I'm trying to get a seperate class, initialized by a form class, to
manipulate certain objects on the form (ex: add to a listbox). The
manipulation will occur via a thread that is not the normal WinForm GUI
thread. How would I go about doing this?

I have this code for cross-thread WinForm manipulation, but only from within
the WinForm class:

public delegate void LogDelegate(string txt);

public void Log(string txt)
{
if (InvokeRequired)
{
Invoke(new LogDelegate(Log), txt);
return;
}

lstLog.Items.Insert(0, DateTime.Now.ToString("T") + " - " + txt);
}

Thanks,
Ian
Nov 17 '05 #1
1 1915
i <ty****@gmail.com> wrote:
I'm trying to get a seperate class, initialized by a form class, to
manipulate certain objects on the form (ex: add to a listbox). The
manipulation will occur via a thread that is not the normal WinForm GUI
thread. How would I go about doing this?

I have this code for cross-thread WinForm manipulation, but only from within
the WinForm class:

public delegate void LogDelegate(string txt);

public void Log(string txt)
{
if (InvokeRequired)
{
Invoke(new LogDelegate(Log), txt);
return;
}

lstLog.Items.Insert(0, DateTime.Now.ToString("T") + " - " + txt);
}


You'd do it in exactly the same way as the above, but using form.Invoke
and form.ListLog or however you want to expose the ListBox to other
classes. Of course, you could make sure that the only thing which has
access to the controls is the form class itself, and just give
thread-safe ways of performing certain operations (as you have above).
Personally, I think that's generally preferable to letting other
objects manipulate the UI, but it depends on the situation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bart Nessux | last post: by
reply views Thread by Ganbold | last post: by
2 posts views Thread by Michal Przytulski | last post: by
9 posts views Thread by Randy | last post: by
6 posts views Thread by ben | last post: by
5 posts views Thread by Nasif | last post: by
14 posts views Thread by =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.