我写了一个这样的模拟程序 ,想要实现在载入界面的时候开始线程1,在线程1中开始线程2并等待线程2结束后再次运行线程1,如此循环往复 ,小弟菜鸟一枚,希望大家可以帮我指出错误,告诉我怎样修改
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool sign = true;
Thread thread;
bool sign2 = false;
Thread thread2;
private void Form1_Load(object sender, EventArgs e)
{
sign = true;
thread = new Thread(delegate() { ReadData(); });
thread2 = new Thread(delegate() { ReadData2(); });
thread.Start();
}
private void ReadData()
{
while (sign)
{
if (checkBox1.Checked == true)
{
textBox1.Invoke(new EventHandler(delegate { textBox1.AppendText("这是线程1的结果\r\n"); }));
thread2.Start();
clearParam();
}
}
}
private void ReadData2()
{
sign2 = true;
textBox1.Invoke(new EventHandler(delegate { textBox1.AppendText("这是线程2的结果\r\n"); }));
}
private void clearParam()
{
sign = false;
thread = new Thread(delegate() { ReadData(); });
thread.Start();
}
}