Interrupting loop

  • Thread starter Sam Sungshik Kong
  • Start date
S

Sam Sungshik Kong

Hello!

I have a form with 2 buttons.

If you click button1, it runs a loop which changes the form's text from 0 to
99.
I added Sleep(500) to make it slow.
If you click button2 while the above routine runs, the loop should stop
there.

Here's my code.

private void button1_Click(object sender, EventArgs e)
{
bContinue = true;
for (int i = 0; i < 100; i++)
{
Text = i.ToString();
Application.DoEvents();
if (!bContinue) break;
Thread.Sleep(500);
}
}

bool bContinue;

private void button2_Click(object sender, EventArgs e)
{
Application.DoEvents(); //this doesn't help whether it's here
not not.
bContinue = false;
}

The problem is that if I click button2, it's ignored and button1 looks to be
clicked instead. If I click button2 one more time, it works as I wish.

What's the problem here?

TIA.
Sam
 
M

mphanke

Hi,

I believe the problem is the Thread.Sleep() call. Since both funcs
reside in the same thread, this might cause a problem. I would seperate
the counter into a seperate thread...

Greets,

Martin
 
M

Mohamoss

hi Sam
What you should do is to create new thread for the function of button one.
Either that or you use the asynchronous model of .net with a callback
delegate. So it will not block the code of button two from setting the
Boolean variable
hope this helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
S

Sam Sungshik Kong

Thanks for the reply.

mphanke said:
Hi,

I believe the problem is the Thread.Sleep() call. Since both funcs reside
in the same thread, this might cause a problem. I would seperate the
counter into a seperate thread...

I removed Thread.Sleep() but the problem is still there.

private void button1_Click(object sender, EventArgs e)
{
bContinue = true;
for (int i = 0; i < int.MaxValue; i++)
{
Text = i.ToString();
Application.DoEvents();
if (!bContinue) break;
//Thread.Sleep(500);
}

}
bool bContinue;

private void button2_Click(object sender, EventArgs e)
{
Application.DoEvents();
bContinue = false;
}

I think creating another thread is the answer. But I want to know what this
is not working.

Sam
 
J

Justin Rogers

private void button1_Click(object sender, EventArgs e)
{
bContinue = true;
for (int i = 0; i < int.MaxValue; i++)
{
Text = i.ToString();
Application.DoEvents();
if (!bContinue) break;
//Thread.Sleep(500);
}

}
bool bContinue;

private void button2_Click(object sender, EventArgs e)
{
Application.DoEvents();
bContinue = false;
}
I think creating another thread is the answer. But I want to know what this is
not working.

Looking at your code, button1_Click occurs on the UI thread, and so you are
hijacking the UI thread. Application.DoEvents() simply runs the message loop
on your now deepened stack (your stack at this point goes from the message
loop to the button click to the message loop)... When button2 is clicked, it'll
get processed in the deepened stack message loop, so you don't need the
DoEvents there, it is superfluous, just set the bContinue and return.

Now there does appear to be a focusing issue such that you have to click the
button2 twice in order to make it stop, but it does stop. I'd highly recommend
moving towards an approach that doesn't deepen the stack the way you have
here. If you do this in multiple controls you'll quickly throw a stack overflow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top