以下程序.在ListBox 一直没有输出.为什么啊?这问题困扰我一个晚上了!
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
Thread newth = new Thread(new ThreadStart(f.insetString));
newth.Start();
}
private void insetString()
{
while (true)
{
this.listBox1.Items.Add("bsbsbsbsbsbsbsbsbs");
Thread.Sleep(1000);
}
}
}
}
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
Thread newth = new Thread(new ThreadStart(f.insetString));
newth.Start();
}
private void insetString()
{
while (true)
{
this.listBox1.Items.Add("bsbsbsbsbsbsbsbsbs");
Thread.Sleep(1000);
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Thread newth = new Thread(new ThreadStart(insetString));
newth.Start();
}
private void insetString()
{
while (true)
{
this.listBox1.Items.Add("bsbsbsbsbsbsbsbsbs");
Thread.Sleep(1000);
}
}
试试
Control.CheckForIllegalCrossThreadCalls = false; 设置了吗
private void button2_Click(object sender, EventArgs e)
{
Thread newth = new Thread(new ThreadStart(this.insetString));
newth.Start(); }
private void insetString()
{
while (true)
{
this.listBox1.Items.Add("bsbsbsbsbsbsbsbsbs");
Thread.Sleep(1000);
}
} private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
加上这句就没问题了