-3

i have a method

 public static string  MyWrite2()
    {
         string p = "hi";
        for (int i = 0; i < 20; i++)
        {
            p += "kk";
        }

        return p;

    }

and i want to write a thread which when i click on btn1 it call MyWrite2 and put the result on label1, how can i do it?

pmn
  • 2,176
  • 6
  • 29
  • 56
  • Any operation that modifies the user interface must be done from the main thread. If you want to use threads at all (instead of Tasks or the TPL) then use them only to compute or process things. When you want to display the result of that computation take a look at [this question](https://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c). – Dirk Jun 17 '14 at 11:29
  • possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – Dirk Jun 17 '14 at 11:30
  • You do tealise that meoth will never return anything but "hikk" – Tony Hopkinson Jun 17 '14 at 11:31

1 Answers1

0

You can use Invoke() method on a label1 and pass a method which does the writing as an argument to the Invoke() method.

Using anonymous method it would be sth like:

label1.Invoke(new MethodInvoker(delegate
{
  label1.Text = MyWrite2();
}
));

In the other thread.

user3613916
  • 232
  • 1
  • 10