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
VulpesPosted Jun 16, 2011, 9:49 AM
Sam HobbsPosted Jun 16, 2011, 10:31 PM
Guest UserPosted Jun 16, 2011, 10:01 PM
Sam HobbsPosted Jun 16, 2011, 8:55 PM
VulpesPosted Jun 16, 2011, 11:23 AM
Guest UserPosted Jun 16, 2011, 10:55 AM
Dan HoPosted Jun 16, 2011, 10:28 AM
Do these solutions have any impact on CPU usage?
Will it require more usage? It looks indeed more complicated at least code-wise.
Pradeep ChandrakerPosted Jun 16, 2011, 10:26 AM
Guest UserPosted Jun 16, 2011, 10:16 AM
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);
}
Guest UserPosted Jun 16, 2011, 10:07 AM
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);
}
}
Dan HoPosted Jun 16, 2011, 9:53 AM
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 UserPosted Jun 16, 2011, 9:48 AM
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 HoPosted Jun 16, 2011, 9:38 AM
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 UserPosted Jun 16, 2011, 9:32 AM
System.Threading.Thread.Sleep(10);