2

So I have this little bit of code here...

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox2.Text==("Tim The Enchanter") && textBox1.Text==("cave100"))
        {
            label2.Visible = true;
            label2.Text = ("Correct");
            label2.ForeColor = System.Drawing.Color.Green;
            System.Threading.Thread.Sleep(1000);
            this.Hide();
            Form2 form2 = new Form2();
            form2.Visible = true;

        }
    }

It is basically a very primitive login screen!

Everything works except for the fact that the form changes to form2 before the label2 text can be seen. I tried to fix this by adding a system wait comand but instead this comes through before the text can be displayed. Once again I am back to where I started.

Any help would be appreciated!

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272

3 Answers3

4

Never use Thread.Sleep for waiting purposes in WinForms.
It blocks GUI thread and your label is not updated / seen by a user.

Of course, there are many workarounds to do it, you can read it here.

The easiest one is to use C# 5.0 async / await functionality:

private async void button1_Click(object sender, EventArgs e)
{
    if (textBox2.Text==("Tim The Enchanter") && textBox1.Text==("cave100"))
    {
        label2.Visible = true;
        label2.Text = ("Correct");
        label2.ForeColor = System.Drawing.Color.Green;
        await Task.Delay(1000);
        this.Hide();
        Form2 form2 = new Form2();
        form2.Visible = true;
    }
}
Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
0

You shouldn't call Thread.Sleep from the UI thread in winforms.

A click event handler needs to do its work and return quickly, because all the form drawing, responding to resize events, responding to user-input, etc is all happening on the same thread. This is the reason you don't see the form repaint itself - it doesn't get a chance, because you suspended the thread it would use to do that.

You need to do this in a more "event-driven" way. For example, you could add a Timer control to the form, with a timeout of 1000ms, and an Elapsed handler that shows/hides the forms.

Then your button handler just sets the timer Enabled to true, and exits.

Remember to stop the timer in the timer's event handler though!

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

Try this:

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox2.Text==("Tim The Enchanter") && textBox1.Text==("cave100"))
        {
            label2.Visible = true;
            label2.Text = ("Correct");
            label2.ForeColor = System.Drawing.Color.Green;

            new Timer{Enabled=true,Interval=100}.Tick += (s,e) =>
            {
              ((Timer)s).Dispose();
              this.Hide();
              Form2 form2 = new Form2();
              form2.Visible = true;
            }
        }
    }

It will give the OS a chance to make modifications to the UI before it shows the instance of Form2

Alastair Brown
  • 1,598
  • 8
  • 12