- ·上一篇内容:使用CFile类读取大文件
- ·下一篇内容:.NET和SQL Server中的“空值”概念
c#中使用多线程
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
Add();
}
private void button2_Click(object sender, System.EventArgs e)
{
Del();
}
private void button3_Click(object sender, System.EventArgs e)
{
while(threads.Count>0)
{
Thread t1 = (Thread)threads[0];
if(t1.IsAlive)
{
t1.Abort();
}
threads.RemoveAt(0);
lock(listView1)
{
listView1.Items.RemoveAt(0);
}
}
}
private void Add()
{
int count = threads.Count;
if(count<10)
{
Thread t = new Thread(new ThreadStart(Process));
t.Start();
threads.Add(t);
lock(listView1)
{
listView1.Items.Insert(count, new ListViewItem(new string[]{count.ToString(),"0"}));
}
}
}
private void Del()
{
int count = threads.Count;
if(count>0)
{
Thread t1 = (Thread)threads[count-1];
if(t1.IsAlive)
{
t1.Abort();
}
threads.RemoveAt(count-1);
lock(listView1)
{
listView1.Items.RemoveAt(count-1);
}
}
}
本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!
微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。
个人成长离不开各位的关注,你的关注就是我继续前行的动力。