
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 1.24 KB | hits: 11 | expires: Never
Delegates in Threads and Annonymous methods - Update controls in new Thread
public partial class DynamicType : Form
{
delegate void Del(String x);
public DynamicType()
{
InitializeComponent();
}
private void DynamicType_Load(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(StartThread));
t.Start();
}
private void StartThread()
{
this.Invoke(new Del(UpdateLabel), new object[] { "Hi" });
}
private void UpdateLabel(String str)
{
label1.Text = str;
}
}
private void StartThread()
{
UpdateLabel("Hi");
}
private void UpdateLabel(String str)
{
Del Label = delegate(String k)
{
label1.Text = k;
};
Label("hi");
}
this.Invoke(new Del(UpdateLabel), new object[] { "Hi" });
Del Label = delegate(String k) { label1.Text = k; };
Label("hi"); // runs in the same thread
Del Label = delegate(String k) { label1.Text = k; };
this.Invoke(Label, new object[] {"hi"}); // runs in the UI thread