Context: I have a form with multiple checkbox "buttons".
In mainform I have a do-while cycle that needs to check form.checkBox.Checked in real time to update some variables.
I tried running the cycle inside an async worker, threading the form2, running the cycle inside the form2, all unsuccessfully.
Faulty Code:
namespace CSharp
{
public partial class MainForm : Form
{
bool exit_state = true;
private BackgroundWorker worker = null;
public MainForm()
{
InitializeComponent();
}
private void bRecord_Click(object sender, EventArgs e)
{
System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".xml", true);
string behavior = null;
exit_state = false;
form2 form = new form2();
form2.ShowDialog();
worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler((state, args) =>
{
do
{
if (form2.checkBox1.Checked) behavior = form2.checkBox1.Text;
if (form2.checkBox2.Checked) behavior = form2.checkBox2.Text;
file.WriteLine(behavior);
}
while(exit_state==false);
});
}
private void bStop_Click(object sender, EventArgs e)
{
exit_state = true;
_worker.CancelAsync();
}
}
}
I believe this is solved using threads for each form, but I lack the knowledge about threads to know how.
EDIT: I realize now that I was not explicit enough and was asking the wrong question so I started a new one.