I have application that have button i want when click this button every 1 second add special text add to a TextBox but when click this button all time wait and enter all text together.
what is problem ?
for example :
private void button2_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random(DateTime.Now.Millisecond);
textBox1.Text = "";
for (int i = 0; i < 10; i++)
{
textBox1.Text += rand.Next(1,10) + "\n";
System.Threading.Thread.Sleep(1000);
}
}
thanks.
You’re sleeping on the UI thread.
This freezes the UI.
Instead, use a
BackgroundWorkerand its progress events.