C#winform实现多线程并行运行,主要功能是有三个listview 讲数据并行填入到listview里面
2个回答
2013-05-12
展开全部
using System; using System.Windows.Forms; using System.Threading; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public delegate void timer(); public Form1() { InitializeComponent(); Thread t = new Thread(TWork); t.Start(); } private void TWork() { // 控制非本线程创建的控件帮助方法 if (this.IsHandleCreated) { listView1.BeginInvoke(new timer(SetListView)); } } private void SetListView() { listView1.Items.Add("aaaa"); } } } 并行就创建3个线程
2013-05-12
展开全部
void a1()
{
checkedListBox1.Items.Clear();
for (int i = 0; i < 10000; i++)
{
checkedListBox1.Items.Add(i.ToString());
checkedListBox1.Update();
Application.DoEvents();
}
}
void a2()
{
checkedListBox2.Items.Clear();
for (int i = 0; i < 10000; i++)
{
checkedListBox2.Items.Add(i.ToString());
checkedListBox2.Update();
Application.DoEvents();
}
}
void a3()
{
checkedListBox3.Items.Clear();
for (int i = 0; i < 10000; i++)
{
checkedListBox3.Items.Add(i.ToString());
checkedListBox3.Update();
Application.DoEvents();
}
}
private void button3_Click(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
System.Threading.Thread t1 = new System.Threading.Thread(a1);
System.Threading.Thread t2 = new System.Threading.Thread(a2);
System.Threading.Thread t3 = new System.Threading.Thread(a3);
t1.Start();
t2.Start();
t3.Start();
}
{
checkedListBox1.Items.Clear();
for (int i = 0; i < 10000; i++)
{
checkedListBox1.Items.Add(i.ToString());
checkedListBox1.Update();
Application.DoEvents();
}
}
void a2()
{
checkedListBox2.Items.Clear();
for (int i = 0; i < 10000; i++)
{
checkedListBox2.Items.Add(i.ToString());
checkedListBox2.Update();
Application.DoEvents();
}
}
void a3()
{
checkedListBox3.Items.Clear();
for (int i = 0; i < 10000; i++)
{
checkedListBox3.Items.Add(i.ToString());
checkedListBox3.Update();
Application.DoEvents();
}
}
private void button3_Click(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
System.Threading.Thread t1 = new System.Threading.Thread(a1);
System.Threading.Thread t2 = new System.Threading.Thread(a2);
System.Threading.Thread t3 = new System.Threading.Thread(a3);
t1.Start();
t2.Start();
t3.Start();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询