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 .....

Hi, try it this way:

public class testing
{
    delegate void mydelegate(string msg);
    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";
         function1(str);
    }
    public void function1(string str)
    {
        if(listBox1.InvokeRequired)
           listBo1x.Inkvoke(new mydelegate(function1), new object[] { str });
        else
           listBox1.Items.Add(str);
   }
}

Calling the primary thread's invoke member executes the function on that thread instead of the new one.

Edited 5 Years Ago by pseudorandom21: n/a

Thanks for reply, Mr. Mitja Bonca and pseudorandom21 ....

However, there is still a thing I want to know :(

Yeah!!!, The coding of Mr. Mitja Bonca is okay. But the thing I really want to know is "why do we need to use Delegate?"If we don't use delegate, it doesn't work. Why?

Mr. pseudorandom21, I want to request respectfully, Could you explain more clearly for your answer? I am little bit confusing ....

Delegates are used to add methods to events dynamically.
Threads run inside of processes, and allow you to run 2 or more tasks at once that share resources.

Another explanation:
Delegates: Basically, a delegate is a method to reference a method. It's like a pointer to a method which you can set it to different methods that match its signature and use it to pass the reference to that method around.

Thread is a sequentual stream of instructions that execute one after another to complete a computation. You can have different threads running simultaneously to accomplish a specific task. A thread runs on a single logical processor.

This question has already been answered. Start a new discussion instead.