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(o bject sender, EventArgs e)
{
bContinue = true;
for (int i = 0; i < 100; i++)
{
Text = i.ToString();
Application.DoE vents();
if (!bContinue) break;
Thread.Sleep(50 0);
}
}
bool bContinue;
private void button2_Click(o bject sender, EventArgs e)
{
Application.DoE vents(); //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 4 1563
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(o bject sender, EventArgs e) { bContinue = true; for (int i = 0; i < 100; i++) { Text = i.ToString(); Application.DoE vents(); if (!bContinue) break; Thread.Sleep(50 0); } }
bool bContinue;
private void button2_Click(o bject sender, EventArgs e) { Application.DoE vents(); //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
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
Thanks for the reply.
"mphanke" <mp*****@nospam .nospam> wrote in message
news:e4******** ******@TK2MSFTN GP12.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(o bject sender, EventArgs e)
{
bContinue = true;
for (int i = 0; i < int.MaxValue; i++)
{
Text = i.ToString();
Application.DoE vents();
if (!bContinue) break;
//Thread.Sleep(50 0);
}
}
bool bContinue;
private void button2_Click(o bject sender, EventArgs e)
{
Application.DoE vents();
bContinue = false;
}
I think creating another thread is the answer. But I want to know what this
is not working.
Sam
> private void button1_Click(o bject sender, EventArgs e) { bContinue = true; for (int i = 0; i < int.MaxValue; i++) { Text = i.ToString(); Application.DoE vents(); if (!bContinue) break; //Thread.Sleep(50 0); }
} bool bContinue;
private void button2_Click(o bject sender, EventArgs e) { Application.DoE vents(); 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.DoE vents() 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 if a player enters
the 'shutdown' command, everything shuts down, but the input thread is
still sitting waiting for enter to be pressed for raw_input. After
enter is pressed, it exits back to the command prompt as it should.
I'm wondering if...
|
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
refresh my gui App so my Form still can react
|
by: William |
last post by:
For the following code snippet:
for ( ;; ) {
numFDInSet = select( fdmax+1, &readfds, NULL, NULL, &tv );
// ...
signal( SIGALRM, alarm_handler );
// ...
|
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. However, once the load starts program excution will
be in the object of my dataload class. And so I suspect
that the users interaction with an interrupt button
(which could set a flag in the load object) will not be
handled.
|
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 novelty.
Do users of pythonD have a coding technique, or a library file to include,
to endow a program with keyboard interrupt-ability?
Endless loops arise when I forget to increment a loop index, or code
incorrectly the end condition test, or...
| |
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 interrupting
Thanks for any response
|
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 onto a
code.InteractiveConsole on the GUI thread. This has the downside that a
silly mistake that creates an infinite loop also hangs the GUI, thus
preventing you from saving your work or exiting the application in any
normal fashion.
As a...
|
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 terminate.
Does this require creating event handlers and such??
|
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 wondering how to recognize
if the user presses an Escape (or even just a Shift key if that is
simpler) so I may then abort the remaining foreground operations. To
recognize a shift key I tried:
if ((Control.ModifierKeys & Keys.Shift) ==...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |