I'm trying to run simulataneous do while loops but whichever loop is reached first is run and the other isn't.
Here's my code;
namespace sync
{
public partial class Form1 : Form
{
int s = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
s = 0;
Thread begin1 = new Thread(start1);
Thread begin2 = new Thread(start2);
begin1.IsBackground = true;
begin2.IsBackground = true;
begin1.Start();
begin2.Start();
}
private void start1()
{
this.BeginInvoke(new MethodInvoker(delegate()
{
do
{
textBox1.AppendText("1");
Thread.Sleep(1000);
Application.DoEvents();
}
while (s == 0);
}));
}
private void start2()
{
this.BeginInvoke(new MethodInvoker(delegate()
{
do
{
textBox2.AppendText("2");
Thread.Sleep(1000);
Application.DoEvents();
}
while (s == 0);
}));
}
private void button2_Click(object sender, System.EventArgs e)
{
s = 1;
}
}
}
The code is simple; button one starts both threads and button 2 stops them.
The threads have the .isbackground property set high thus they should both run fine.
Any pointers would be greatly appreciated.