problem with this?

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

I was reading the "thread problem" post and maybe it has something to do
with this, but I didn't quite understand it all. Here is the code I wrote:

private void btnStart_Click(object sender, EventArgs e)
{
int time = Int32.Parse(txtTime.Text);
Thread.Sleep(60000);

for (int i = time - 1; i > 0; i--)
{
txtMessage.Text = i + " minutes remaining.";
Thread.Sleep(60000);
}

txtMessage.Text = time + " minutes have elapsed.";

for (int i = 3; i > 0; i--)
{
Console.Beep();
Thread.Sleep(1000);
}

Originally this was a console application, and I just changed the
txtMessage.Text parts from the original Console.WriteLine parts.

What happens is that it will run through to the end (although sometimes
I get "Not Responding") and it will display the final message and the
beeps, but it does not run through the first for loop, which shows
minutes remaining. The debugger also seems to get hung up when I use it,
and I can't find a way to step over the Thread.Sleep method, so it sort
of kills the application, even in debug mode.
 
John Salerno said:
I was reading the "thread problem" post and maybe it has something to do
with this, but I didn't quite understand it all. Here is the code I wrote:

private void btnStart_Click(object sender, EventArgs e)
{
int time = Int32.Parse(txtTime.Text);
Thread.Sleep(60000);

for (int i = time - 1; i > 0; i--)
{
txtMessage.Text = i + " minutes remaining.";
Thread.Sleep(60000);
}

txtMessage.Text = time + " minutes have elapsed.";

for (int i = 3; i > 0; i--)
{
Console.Beep();
Thread.Sleep(1000);
}

Originally this was a console application, and I just changed the
txtMessage.Text parts from the original Console.WriteLine parts.

What happens is that it will run through to the end (although sometimes
I get "Not Responding") and it will display the final message and the
beeps, but it does not run through the first for loop, which shows
minutes remaining. The debugger also seems to get hung up when I use it,
and I can't find a way to step over the Thread.Sleep method, so it sort
of kills the application, even in debug mode.

Yes, it's running through the first for loop - but because you're
running on the UI thread, it's not actually getting a chance to update
the UI.

One of the golden rules of UI threading: don't hog the UI thread.
 
Jon said:
Yes, it's running through the first for loop - but because you're
running on the UI thread, it's not actually getting a chance to update
the UI.

One of the golden rules of UI threading: don't hog the UI thread.

Hmm, I figured the threading thing might be a problem when I converted
it to a WinForm app. Is there another way to create this delay for my timer?
 
Back
Top