我想利用线程循环将父窗体的东西传递到子窗体当中
我试了一下,发现当子窗体打开之后,线程便不再运行,要怎么才能打开子窗体后,父窗体的线程能够继续运行呢?
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
//Form1 f1 = new Form1();
timer1.Enabled = true;
} private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = Form1.a;
}
}
}
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string a;
private void button1_Click(object sender, EventArgs e)
{
Thread tt = new Thread(t);
tt.IsBackground = true;
tt.Start();
Form2 f2 = new Form2();
f2.Show(); } private void Form1_Load(object sender, EventArgs e)
{
} void t()
{
while (true)
{
a = (System.Int32 .Parse ( textBox1.Text)+1).ToString ();
}
}
}
}这是我自己实验的程序,当我运行出来,在Form2上的Label1中显示的是2,也就是说它只进行了一次加1,没有继续再往上加
要怎么弄才能在Form2显示的时候,Form1的线程能够继续运行呢?
我试了一下,发现当子窗体打开之后,线程便不再运行,要怎么才能打开子窗体后,父窗体的线程能够继续运行呢?
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
//Form1 f1 = new Form1();
timer1.Enabled = true;
} private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = Form1.a;
}
}
}
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string a;
private void button1_Click(object sender, EventArgs e)
{
Thread tt = new Thread(t);
tt.IsBackground = true;
tt.Start();
Form2 f2 = new Form2();
f2.Show(); } private void Form1_Load(object sender, EventArgs e)
{
} void t()
{
while (true)
{
a = (System.Int32 .Parse ( textBox1.Text)+1).ToString ();
}
}
}
}这是我自己实验的程序,当我运行出来,在Form2上的Label1中显示的是2,也就是说它只进行了一次加1,没有继续再往上加
要怎么弄才能在Form2显示的时候,Form1的线程能够继续运行呢?
你FORM1里面的textbox1.text是没有变化的
所以a = (System.Int32 .Parse ( textBox1.Text)+1).ToString ();是定值
->
f2.ShowDialog();
private void button1_Click(object sender, EventArgs e)
{
Thread tt = new Thread(t);
tt.IsBackground = true;
tt.Start();
Form2 f2 = new Form2();
f2.Show();
}
当你程序执行f2.Show()后,这段代码就结束了, tt应该被自动释放了,线程大约被杀死了。