Hello,
first off, im a real novice when it comes down to programming so dont wonder if my approach is totally wrong.
I have a CheckBox and want to implement a do while loop that will run as long as the checkbox stays checked.
Once I uncheck the checkbox, it should jump out of the loop and stop.
Since I'm a beginner I really dont know how to approach this. I've tried the following but once in the loop, it will hang and stay in the loop forever even if I uncheck the box and I'll have to shut down the process via task manager, heres the snippet:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
do
{
textBox1.AppendText("On");
} while (checkBox1.Checked);
}
else if (!checkBox1.Checked)
{
textBox1.AppendText("Off");
}
}
The AppendText thing makes no sense but its just for testing purposes at the moment to see if I can get it working as I want.
I would be glad if anyone could help me to solve this issue.
Thanks.
Loading

VulpesRank #55 · 29.2K reputationPosted Jun 16, 2011, 5:49 AM EDT
Guest UserRank #1,422 · 1.2K reputationPosted Jun 16, 2011, 5:32 AM EDT
System.Threading.Thread.Sleep(10);
Dan Ho12 reputationPosted Jun 16, 2011, 5:38 AM EDT
Unfortunately, it didnt change anything apart from the fact that the text output is slower now.
I am still unable to uncheck the box even during the period of Thread Sleep.
Guest UserRank #1,422 · 1.2K reputationPosted Jun 16, 2011, 5:48 AM EDT
You have to move the loop outside of the event handler. The reason is that when you check the box for the 1st time, you enter the event handler and start the loop, but you never exit the handler. This means that the checkbox can't respond to the next click to uncheck the box.
Dan Ho12 reputationPosted Jun 16, 2011, 5:53 AM EDT
thanks a lot, it worked.
Amazing what a simple command can achieve.
@ John Penn
I thought about this aswell and this is most likely an alternative solution opposed to the one from Vulpes. I'll give it a try aswell.
Thanks for your replies guys.
Guest UserRank #1,422 · 1.2K reputationPosted Jun 16, 2011, 6:07 AM EDT
public partial class Form1 : Form
{
private System.Threading.Thread t = null;
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (t == null)
{
t = new System.Threading.Thread(TextAppender);
t.Start();
}
}
else
{
t.Join(); // wait for TextAppender to finish its current iteration
t = null;
textBox1.AppendText("Off");
}
}
private void TextAppender()
{
do
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new MethodInvoker(delegate { textBox1.AppendText("On"); }));
}
System.Threading.Thread.Sleep(1); // sleep so that UI can respond to clicks
} while (checkBox1.Checked);
}
}
Guest UserRank #1,422 · 1.2K reputationPosted Jun 16, 2011, 6:16 AM EDT
private void TextAppender()
{
MethodInvoker methodInvoker = new MethodInvoker(delegate { textBox1.AppendText("On"); });
do
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(methodInvoker);
}
System.Threading.Thread.Sleep(1); // sleep so that UI can respond to clicks
} while (checkBox1.Checked);
}
Pradeep ChandrakerRank #941 · 2.2K reputationPosted Jun 16, 2011, 6:26 AM EDT
Dan Ho12 reputationPosted Jun 16, 2011, 6:28 AM EDT
Do these solutions have any impact on CPU usage?
Will it require more usage? It looks indeed more complicated at least code-wise.
Guest UserRank #1,422 · 1.2K reputationPosted Jun 16, 2011, 6:55 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jun 16, 2011, 7:23 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Jun 16, 2011, 4:55 PM EDT
Guest UserRank #1,422 · 1.2K reputationPosted Jun 16, 2011, 6:01 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Jun 16, 2011, 6:31 PM EDT