473,508 Members | 2,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interrupting loop

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
Nov 16 '05 #1
4 1552
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

Sam Sungshik Kong wrote:
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

Nov 16 '05 #2
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

Nov 16 '05 #3
Thanks for the reply.

"mphanke" <mp*****@nospam.nospam> wrote in message
news:e4**************@TK2MSFTNGP12.phx.gbl...
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
Nov 16 '05 #4
> 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.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7224
by: J. W. McCall | last post by:
I'm working on a MUD server and I have a thread that gets keyboard input so that you can enter commands from the command line while it's in its main server loop. Everything works fine except that...
1
1740
by: stephan | last post by:
Hi, Im am using PyRun_SimpleString() inside a BCB 5.0 GUI app on win32. All works fine. The PyRun_SimpleStript() runs a piece of python code which calls often a callback function to...
1
3370
by: William | last post by:
For the following code snippet: for ( ;; ) { numFDInSet = select( fdmax+1, &readfds, NULL, NULL, &tv ); // ... signal( SIGALRM, alarm_handler ); // ...
6
1215
by: Frank | last post by:
I have made an application that loads data through an OleDbDatareader. I would like the user to be able to interupt the load. Now, I could of course include an interrupt button in the form....
1
1307
by: John Savage | last post by:
I'm finding that many times when I inadvertently code an endless loop in pythonD code, on MSDOS, execution cannot be interrupted by ctrl-C and I am forced to reboot and this is rapidly loses its...
8
1207
by: andreas | last post by:
When I do a long calculation is there a possibility to interrupt this calculation properly? For i as integer = 1 to 100000000 do something next and retrieve the i at the moment of the...
0
1302
by: johan2sson | last post by:
I have embedded a python console in a plugin to a Windows application. I do not control the application, but the plugin is mine. Since the application is not thread-safe, I am push()-ing each line...
0
1150
by: anonymous | last post by:
I have a simple incrementing loop, which if a button is clicked I want the loop to stop completely. The loop is in its own simple method. And when a button is clicked I want this loop to...
13
1745
by: michael sorens | last post by:
I have a lengthy sequence of operations that are executed and reported on in a status window in a Windows Form application. Some work is done by background threads but other work is not. I am...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.