Stop while loop
-
How can i stop an while loop by clicking a button? eg: A while loop is executing, i want to stop the while loop from outside like a button click Thankyou YPKI
-
How can i stop an while loop by clicking a button? eg: A while loop is executing, i want to stop the while loop from outside like a button click Thankyou YPKI
-
How can i stop an while loop by clicking a button? eg: A while loop is executing, i want to stop the while loop from outside like a button click Thankyou YPKI
I don't know do you want to break immediately, but something like this may help
bool stop = false;
while(!stop)
{
..dosomething
}or
while(true)
{
if(stop)
break;
}and on your button_click
{
stop = true;
}hope it helps
-
How can i stop an while loop by clicking a button? eg: A while loop is executing, i want to stop the while loop from outside like a button click Thankyou YPKI
A bit vague. Is your while loop executing in a background thread? If it's executing on your GUI thread and your not calling Application.DoEvents it won't ever break!
Regards, Rob Philpott.
-
A bit vague. Is your while loop executing in a background thread? If it's executing on your GUI thread and your not calling Application.DoEvents it won't ever break!
Regards, Rob Philpott.
while loop is runnung normally, not in thread. how can i stop using Application.DoEvents
-
you must put that loop in a new thread. then use a flag, like _isButtonClicked. If yes, then break.
how to put a while loop in a thread?
-
while loop is runnung normally, not in thread. how can i stop using Application.DoEvents
Create a boolean member:
private bool _running = true;
In the button click event set it to false:
_running = false;
Make your while loop look like this
while (_running)
{
Application.DoEvents();
// your stuff here
}Regards, Rob Philpott.
-
how to put a while loop in a thread?
The easiest way to put it in a background thread is to make sure you have a method signature that take one parameter of type object and then use
ThreadPool.QueueUserWorkItem(new WaitCallback(method))
- "method" simply being the name of the method you wish to call.bool stop;
void method(object dummy)
{
while (!stop)
{
...
}
}However, be aware that the code in method should not use any references to any controls. By default, doing so will result in an exception. You can make this go away by setting
Control.CheckForIllegalCrossThreadCalls
tofalse
, but it's not a good practice to do so, I think because the controls aren't designed to be thread-safe. Depending on what you're doing, you may or may not decide that you don't care and just use the controls from the background thread anyway. I've done it many times (in apps for my own use only, where I don't really care if this one day leads to some odd behavior of a button because two threads tried to update it's text property at once, resulting in something weird and maybe beautiful) and I haven't had any problems with it, but there is undoubtedly a good reason why Microsoft decided to disallow this by default. -
Create a boolean member:
private bool _running = true;
In the button click event set it to false:
_running = false;
Make your while loop look like this
while (_running)
{
Application.DoEvents();
// your stuff here
}Regards, Rob Philpott.
Problem with this is that there won't be any click events, because it won't check this. You can better use a thread and in this thread you put your code. Something like this (out of my head):
private bool _running = true;
public static void main(args[])
{
Thread t = new Thread(threadFunction);
t.Start();
}public void threadFunction()
{
while(_running)
{
// your stuff here;
}
}public void Button1_Clicked(object sender, EventArgs e)
{
_running = false;
}Don't know if the 'thread' part is correct, the rest is ok ;P
-
Problem with this is that there won't be any click events, because it won't check this. You can better use a thread and in this thread you put your code. Something like this (out of my head):
private bool _running = true;
public static void main(args[])
{
Thread t = new Thread(threadFunction);
t.Start();
}public void threadFunction()
{
while(_running)
{
// your stuff here;
}
}public void Button1_Clicked(object sender, EventArgs e)
{
_running = false;
}Don't know if the 'thread' part is correct, the rest is ok ;P
Actually, its perfectly valid. The Application.DoEvents keeps the message pump pumping and is the way mutitasking used to work in the Windows 3.x world. Admittedly, I prefer your way of doing it but the gentleman suggested he didn't want to use background threads.
Regards, Rob Philpott.
-
Actually, its perfectly valid. The Application.DoEvents keeps the message pump pumping and is the way mutitasking used to work in the Windows 3.x world. Admittedly, I prefer your way of doing it but the gentleman suggested he didn't want to use background threads.
Regards, Rob Philpott.