It is OK, if you write like this below :
delegate void mydelegate();
public class testing
{
mydelegate d1;
string str ;
public void btnStart_Click(object sender, EventArgs e) // Click Event
{
ThreadStart ts = new ThreadStart(StartServer);
Thread t = new Thread(ts);
t.Start();
}
public void StartServer()
{
str = "Hello";
d1 = new mydelegate(function1); // specified thread
listBox1.Invoke(d1); // execute the specified delegate on Thread
}
public void function1()
{
listbox1.Items.Add(str);
}
}
However, It is NOT OK, if you write like this below :
class testing
{
public void btnStart_Click(object sender, EventArgs e)
{
ThreadStart ts = new ThreadStart(StartServer);
Thread t = new Thread(ts);
t.Start();
}
public void StartServer()
{
string str = "Hello";
listbox1.Items.Add(str); // add data into ListBox directly without using delegate BUT it will occur exception !!!
}
}
The answer I want to know is
1. "Why do we need to use delegate when we get data from components(Eg. ListBox) or insert into components within a thread?"
2. listbox1.Invoke(delegateobj) // why do we need to call Invoke() method? If we use delegate without using thread, I think we don't need to call Invoke() method !!!
3. What is the relationship between Thread and Delegate when insert or get data from a component within a thread?
Thank you ...... Please help me .....