Is there any way to do any cycle and break it until another button in the WinForms is pressed ?
do
{
//Action
} while ("Button Stop is pressed");
Is there any way to do any cycle and break it until another button in the WinForms is pressed ?
do
{
//Action
} while ("Button Stop is pressed");
The best way that you can do this without blocking the gui is to make use of threads, for instance:
Thread t = new Thread (delegate() {
while(true) {
What you want to happen inside the loop in here;
Thread.Sleep(500); // this is useful for keeping the headroom of the CPU
}
});
Start the Thread this way:
t.Start();
When the stop button is pressed abort the thread this way:
t.Abort();
This keeps the gui alive.
There are two possible cases:
1-You are running that loop in the main thread, then the answer is no, if you block the main thread events will not fire and can't catch button click.
2-You are running that loop in a worker thread, in this case you can create a boolean outside the function set to true, check this boolean in your loop and set it to false in the click event of your button.