1

I have:

private void button1_MouseEnter(object sender, EventArgs e)
{
    for (int i = 0; i > 2; i++) 
    {     
        button1.Content = Convert.ToString(i);
        System.Threading.Thread.Sleep(1000);     
    }

    tekst.Text = "Mouse Enter";
}

When I enter on Button I see only Mouse Enter, but Content on Button don't change. Why? What I can do wrong?

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
user1638121
  • 167
  • 1
  • 5
  • 16
  • What is `tekst`? and can you be more clear on what your problem is? I cannot understand what you are trying to do. – ryrich Mar 07 '13 at 18:55
  • 2
    The loop condition is wrong. It should be `i < 2`. Having said that, you should not have the loop at all. You should instead use a timer, for example a [DispatcherTimer](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx). – Clemens Mar 07 '13 at 19:13
  • 1
    If you sleep on your UI thread the UI will have difficulty updating. – Curt Nichols Mar 07 '13 at 19:19

2 Answers2

2

Hi is your for loop correct? It should be i<2 instead of i>2

 for (int i = 0; i < 2; i++)
        {
yo chauhan
  • 12,079
  • 4
  • 39
  • 58
0

Your for loop never execute because you have wrong condition, change it to following code:

for (int i = 0; i < 2; i++)

Also you should use BackgroundWorker (msdn) to update your GUI dynamicly.

private void button1_MouseEnter(object sender, MouseEventArgs e)
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += delegate
    {
        for (int i = 0; i < 2; i++)
        {
            this.Dispatcher.Invoke((Action)(() => { btn.Content = Convert.ToString(i); }));
            System.Threading.Thread.Sleep(1000);
        }                
    };
    worker.RunWorkerCompleted += delegate { tekst.Text = "Mouse Enter"; };
    worker.RunWorkerAsync();
}
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65