namespace WindowsForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread thread; private void button1_Click(object sender, EventArgs e)
{
//主线程
for (int i = 100; i < 150; i++)
{
listBox1.Items.Add(i);
Thread.Sleep(100);
listBox1.Refresh();
} //新开线程
ThreadStart start = new ThreadStart(Start);
thread = new Thread(start);
thread.Start(); //Thread cThread = new Thread(new ThreadStart(Start));
//cThread.Start();
} delegate void aa();
private void Start()
{
aa a = delegate()
{
for (int i = 0; i < 50; i++)
{
listBox1.Items.Add(i);
Thread.Sleep(100);
listBox1.Refresh();
}
};
listBox1.Invoke(a);
} }
}
想让主线程和新开线程同时交替往listBox1中填充数据,应该怎么做?我上面的写法错在哪了?
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread thread; private void button1_Click(object sender, EventArgs e)
{
//主线程
for (int i = 100; i < 150; i++)
{
listBox1.Items.Add(i);
Thread.Sleep(100);
listBox1.Refresh();
} //新开线程
ThreadStart start = new ThreadStart(Start);
thread = new Thread(start);
thread.Start(); //Thread cThread = new Thread(new ThreadStart(Start));
//cThread.Start();
} delegate void aa();
private void Start()
{
aa a = delegate()
{
for (int i = 0; i < 50; i++)
{
listBox1.Items.Add(i);
Thread.Sleep(100);
listBox1.Refresh();
}
};
listBox1.Invoke(a);
} }
}
想让主线程和新开线程同时交替往listBox1中填充数据,应该怎么做?我上面的写法错在哪了?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货