Problem:
======
Accesseing UI element from Chield Thread.
Detail:
====
Basically UI thread block its control. So if you normally try to access control property from different thread then exception will raise. So you need to do work-around.
Solution:
=======
Indivisual Invokation is required to solve the problem. Every ui control has a boolean property name InvokeRequired and a method invoke(delegate). You first need to check its invokation is required or not. If yes then call invoke() with a delegate.
Code Objective:
===========
I just want to change a button caption from a chield thread.
Code Sample:
=========
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//declare a delegate
public delegate void SetData();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Create a chield thread.
Thread t = new Thread(
new ThreadStart(SetCaptionToButton));
//Run the thread.
t.Start();
}
private void SetCaptionToButton()
{
//Create a delegate
Delegate del = Delegate.CreateDelegate(typeof(SetData),
this, "SetCaptionToButton");
if (button1.InvokeRequired)
{
//Either 1 or 2 the following statement you can use.
button1.Invoke(del); //1
button1.Invoke(new MethodInvoker
(SetCaptionToButton)); //2
}
button1.Text = "Click ME!";
}
}
}